Search code examples
arraysstringgoogle-apps-scriptdata-manipulation

Adding Text to Cells with Text - Apps Script


Simple enough, I want to add "navigatela.lacity.org/" to the beginning of each cell in column G programmatically (bar the header). What different ways could I go about doing this?

Thank you! Your help is greatly appreciated :D

enter image description here


Solution

  • I believe your goal is as follows.

    • You want to add a text of navigatela.lacity.org/ to the top of the value in the column "G".
    • You want to achieve this using Google Apps Script.

    In this case, how about the following sample script?

    Sample script:

    function myFunction() {
      const add = "navigatela.lacity.org/"; // This is from your question.
      const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // Please set your sheet name.
      sheet.getRange("G2:G" + sheet.getLastRow()).createTextFinder("(^.*$)").useRegularExpression(true).replaceAllWith(`${add}$1`);
    }
    
    • When this script is run, the text of navigatela.lacity.org/ is added to the top of the value in the column "G". In this case, this text is not added to the empty cells.

    References: