Home > Back-end >  Adding Text to Cells with Text - Apps Script
Adding Text to Cells with Text - Apps Script

Time:07-08

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

CodePudding user response:

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:

CodePudding user response:

Try this:

function addToBeginning() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Enter Your Sheet Name");
  const vs = sh.getRange(2, 7, sh.getLastrow() - 1).getValues().flat();
  let oA = vs.map(e => `navigatela.lacity.org/${e}`);
  sh.getRange(2, 7, oA.length, oA[0].length).setValues(oA);
}
  • Related