Home > Mobile >  Apply Apps Script Function to Other Rows in Google Sheets
Apply Apps Script Function to Other Rows in Google Sheets

Time:10-30

I am just wondering if anyone would offer suggestions to me about how to apply a function in my script to other rows in the sheet.

Basically I have a script which copies a cell containing a date to another cell when the user makes the checkbox "TRUE". It works fine on my test row but I am unsure how to make this apply to the remaining rows.

I was thinking I need to make a loop to iterate through the other rows or apply a getLastRow function, or do I apply this to the entire sheet minus the top row.... I have over thought myself into confusion over what is probably really simple.

New to google scripts, is there a best practice for this type of thing?

Here is my function:

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSheet();
  var date = new Date( ss.getRange(2,7).getValue());

  if (e.range.columnStart != 1 || e.value != "TRUE") return;
  ss.getRange(2,4).setValue(new Date(date.setDate(date.getDate())));
}

It is my first script so di nit be to harsh ;) Thanks,

CodePudding user response:

SUGGESTION:

You can try this implementation by getting the current selected row using e.range.rowStart:

function onEdit(e) {
  var ss = e.source.getActiveSheet();
  var date = new Date(ss.getRange(e.range.rowStart,7).getValue());

  if (e.range.columnStart != 1 || e.value != "TRUE") return;
  ss.getRange(e.range.rowStart,4).setValue(new Date(date.setDate(date.getDate())));
}

Sample Demonstration:

enter image description here

  • Related