Forum Discussion

Bkkendig's avatar
Bkkendig
Copper Contributor
May 25, 2023
Solved

Excel Script to Remove Carriage Returns

I am trying to write an excel online script to remove carriage returns. Writing a macro to do this is in the desktop version of excel is easy via "cntl+j" using the replace drop down. However, I find...
  • NikolinoDE's avatar
    May 25, 2023

    Bkkendig 

    To remove carriage returns in Excel Online using Office Scripts, you can utilize the replaceAll method of the Range object.

    Here's an example script that removes carriage returns from a specific range of cells:

    function main(workbook: ExcelScript.Workbook) {
      let sheet = workbook.getActiveWorksheet();
      let range = sheet.getRange("A1:A10"); // Specify the range where your data is located
    
      // Replace carriage returns with an empty string
      range.replaceAll("\r", "");
    
      // Save the changes
      workbook.save();
    }

    To use this script:

    To use this script:

    1. Open the Excel Online workbook where you want to remove carriage returns.
    2. Click on the "Automate" tab in the ribbon.
    3. Click on "Script Lab" to open the Office Scripts editor.
    4. In the editor, create a new script and paste the provided code.
    5. Adjust the range in the getRange method to match the actual range of your data.
    6. Click the "Run" button to execute the script.

     

    The script will replace all carriage returns (\r) with an empty string in the specified range. Make sure to save the changes after running the script.

Resources