How to access Spreadsheet from a Google Forms submit trigger function

Wigmore

I have a function in Google Forms script to add a Unique ID to the row being added to the associated Sheet. This function is triggered on submission of the form.

Here's one version of the script I've been playing around with:-

function onFormSubmit(e) {
  // Get the active sheet
  var sheet = SpreadsheetApp.getActiveSheet();
  // Get the active row
  var row = sheet.getActiveCell().getRowIndex();
  // Get the next ID value.  
  var id = sheet.getRange("Z4").getValue();
  // Check of ID column is empty
  if (sheet.getRange(row, 1).getValue() == "") {
    // Set new ID value
    sheet.getRange(row, 1).setValue(id);
    sheet.getRange("Z4").setValue("Z4"+1)
  }
}

On debugging I get the message:-

TypeError: Cannot call method "getActiveCell" of null. (line 5, file "CreateID")

I've tried cropping the code right down to just a simple setValue, and I get the same issue - "cannot call method...", with pretty much every line except the getActiveSheet.

The trigger of the function works ok, as I get notifications to say that the function itself had failed to execute successfully. I've looked online, tried a few things, but can't find a solution as yet.

So what I'm really after is the correct method of accessing the spreadsheet that the form is posting to. If SpreadsheetApp.getActiveSheet() isn't the right method, what is?

Totally new to this script, last programmed in PLI(!), sorry. Any pointers to existing solutions or other, would be appreciated.

Mogsdad

When a form is submitted, the trigger function has no active spreadsheet, nor will it have an active cell. (It's not associated with a Sheets UI, so those concepts are meaningless.)

However, the event parameter, e, will provide information about the row that has been added by the Form. See Google Sheets events:

screenshot

e.range contains a Range object covering the cells that have been filled by the form submission that triggered your function. You can backtrack from there to get the sheet.

sheet = e.range.getSheet();

You function becomes something like this:

function onFormSubmit(e) {
  // Get the active sheet
  var sheet = e.range.getSheet();
  // Get the active row
  var row = e.range.getRowIndex();
  // Get the next ID value.  
  var id = sheet.getRange("Z4").getValue();
  // Check of ID column is empty
  if (sheet.getRange(row, 1).getValue() == "") {
    // Set new ID value
    sheet.getRange(row, 1).setValue(id);
    sheet.getRange("Z4").setValue("Z4"+1)
  }
}

Now, you have other problems to deal with.

  • sheet.getRange(row, 1).getValue() is not necessary; that value has just been handed to you as e.values[0].

  • However, on a form submission, the first column contains a Timestamp, so it won't ever be empty. If your "ID" value is the first question on the form, then it's actually in column 2, or e.values[1].

  • The cell Z4 will likely move on you, as form submissions insert new rows into the sheet. It would be better to pull that value from a different sheet - or better yet use the Properties Service to manage it.

To make use of the event, you'll need to simulate it for testing. Read over How can I test a trigger function in GAS?.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to submit to Google Forms?

From Dev

How does google know which onsubmit trigger to execute when a form is submitted if you have multiple forms sending responses to a single spreadsheet

From Dev

How does google know which onsubmit trigger to execute when a form is submitted if you have multiple forms sending responses to a single spreadsheet

From Dev

How to update a Google Forms dropdown list based off an existing spreadsheet?

From Dev

How to open access in google spreadsheet for a google api project?

From Dev

Xpath: How to extract values from URL in Google spreadsheet using importXML function?

From Dev

Trigger places_changed in Google maps from submit button

From Dev

how to supply a google access token in order to retrieve user spreadsheet list?

From Dev

how to supply a google access token in order to retrieve user spreadsheet list?

From Dev

How to access a specific Spreadsheet?

From Dev

Google Spreadsheet custom function to add csv in spreadsheet

From Dev

Update Google Forms field based on dynamic data from a Google Sheets spreadsheet

From Dev

Omit empty cells in a range when updating a list in Google Forms from Spreadsheet range in Google Apps Script

From Dev

How to trigger a jquery function from a jquery function

From Dev

Google Spreadsheet GAS Trigger not firing for anonymous editors

From Dev

Create time trigger with Google Script and spreadsheet

From Dev

Time-driven trigger not working in Google Spreadsheet

From Dev

ReactJS: how to trigger form submit event from another event

From Dev

How to use Google Spreadsheet ADDRESS function to create a range

From Dev

Disable submit button in Google Forms?

From Dev

submit data from form into spreadsheet table

From Dev

Google Apps Script: How to acess an array values from a submit function in a checkBox list?

From Dev

How to stop one Submit button from running the Submit Click function

From Dev

How to change Submit button text from a view in crispy-forms?

From Dev

How to change Submit button text from a view in crispy-forms?

From Dev

Using a Custom Function in a Google Spreadsheet?

From Dev

how to access $error from nested forms

From Dev

How to get the Worksheet ID from a Google Spreadsheet with python?

From Dev

how return data from HTML service (UI) to google spreadsheet

Related Related

  1. 1

    How to submit to Google Forms?

  2. 2

    How does google know which onsubmit trigger to execute when a form is submitted if you have multiple forms sending responses to a single spreadsheet

  3. 3

    How does google know which onsubmit trigger to execute when a form is submitted if you have multiple forms sending responses to a single spreadsheet

  4. 4

    How to update a Google Forms dropdown list based off an existing spreadsheet?

  5. 5

    How to open access in google spreadsheet for a google api project?

  6. 6

    Xpath: How to extract values from URL in Google spreadsheet using importXML function?

  7. 7

    Trigger places_changed in Google maps from submit button

  8. 8

    how to supply a google access token in order to retrieve user spreadsheet list?

  9. 9

    how to supply a google access token in order to retrieve user spreadsheet list?

  10. 10

    How to access a specific Spreadsheet?

  11. 11

    Google Spreadsheet custom function to add csv in spreadsheet

  12. 12

    Update Google Forms field based on dynamic data from a Google Sheets spreadsheet

  13. 13

    Omit empty cells in a range when updating a list in Google Forms from Spreadsheet range in Google Apps Script

  14. 14

    How to trigger a jquery function from a jquery function

  15. 15

    Google Spreadsheet GAS Trigger not firing for anonymous editors

  16. 16

    Create time trigger with Google Script and spreadsheet

  17. 17

    Time-driven trigger not working in Google Spreadsheet

  18. 18

    ReactJS: how to trigger form submit event from another event

  19. 19

    How to use Google Spreadsheet ADDRESS function to create a range

  20. 20

    Disable submit button in Google Forms?

  21. 21

    submit data from form into spreadsheet table

  22. 22

    Google Apps Script: How to acess an array values from a submit function in a checkBox list?

  23. 23

    How to stop one Submit button from running the Submit Click function

  24. 24

    How to change Submit button text from a view in crispy-forms?

  25. 25

    How to change Submit button text from a view in crispy-forms?

  26. 26

    Using a Custom Function in a Google Spreadsheet?

  27. 27

    how to access $error from nested forms

  28. 28

    How to get the Worksheet ID from a Google Spreadsheet with python?

  29. 29

    how return data from HTML service (UI) to google spreadsheet

HotTag

Archive