looping a script

21 views
Skip to first unread message

Aaron Marsh

unread,
Jun 16, 2020, 5:06:49 PM6/16/20
to Google Apps Script Community
Hi everyone,

I have this simple script which checks if cell F10 is higher than 0 and displays a message if it is and then sets the active cell to nothing if it occurs. (F10 is just a sum of F11:F50)

I'd like it to also do the same thing for the next column so just applying the same rules to G10, H10, I10 etc but I don't want to duplicate the block loads of times. I'm thinking there is some loop I can add here to check them all but I don't know where to start as I'm new to this.

Please can anyone help? My script up to now is below and this currently works fine for cell F10.

Many Thanks


function onEdit(e){
  var sheet = SpreadsheetApp.getActiveSheet();
  var getActiveCell = sheet.getActiveCell();
  var getNumberCell = sheet.getRange("F10").getValue();
  
  if (getNumberCell > 0){Browser.msgBox('value exceeded');
                      (getActiveCell.setValue(""));
}
}


adam....@igbis.edu.my

unread,
Jun 22, 2020, 7:08:44 AM6/22/20
to Google Apps Script Community
Hi there!

A basic way to get a loop going for this would be something like this. If you are using the V8 engine:

const sheet = SpreadsheetApp.getActiveSheet();
const getActiveCell = sheet.getActiveCell();
const targetCells = ["F10", "G10", "H10"];
for (const cell of targetCells) {
    const getNumberCell = sheet.getRange(cell).getValue();
   if (getNumberCell > 0) {
      // yes
      getActiveCell.setValue("");
   } else {
     // no
Reply all
Reply to author
Forward
0 new messages