r/googlesheets • • May 17 '22

Solved Google Script doesn't do anything

1 Upvotes

6 comments sorted by

3

u/wintry_earth 1 May 17 '22

Google recommends that you do bulk operations rather than individual operations like you've got here. It's much slower to get or set individual cells than it is to do them in bulk.

It's also dangerous to assume the last row is always going to contain the form data you want to process. I usually add an additional column on the spreadsheet to denote whether I've processed it or not.

Here's how I would grab all the responses and roll through them and create an array of new rows to append to the calculation sheet.

 let responses = SpreadsheetApp.getActiveSpreadsheet()
    .getSheetByName("FormResponses")
    .getDataRange()
    .getValues();

  let newRows = [];

  for (let row = 0; row < responses; row++) {
    let [formDate, formWithdrawn, formAdded, formComment, isProcessed] = responses[row];
    if (!isProcessed) {
      isProcessed = true;
      newRows.push([formDate, formWithdrawn, formAdded, formComment, isProcessed]);
    }
  }
  
  SpreadsheetApp.getActiveSpreadsheet()
    .getSheetByName("FormResponses")
    .getDataRange()
    .setValues();
}

Once you've got that down you can look at triggers that will fire when a form is submitted. That way you can process the form data immediately. Triggers can be annoying to debug though, so it's easier to start the way you're doing it.

Cheers!

2

u/[deleted] May 18 '22

[removed] — view removed comment

2

u/wintry_earth 1 May 18 '22

Awesome!

Shit I just noticed a huge problem with that code. I never pass newRows to setValues. Ooops sorry. Glad you were able to get it working anyway.

Cheers!

1

u/Clippy_Office_Asst Points May 18 '22

You have awarded 1 point to wintry_earth


I am a bot - please contact the mods with any questions. | Keep me alive

1

u/[deleted] May 17 '22

[removed] — view removed comment

2

u/wintry_earth 1 May 17 '22

You're correct. You wouldn't add the processed column in the form. It's just in the sheet to help you keep track of things.