user3569641
user3569641

Reputation: 922

How to check if there's new data added on Google Sheet

I am just new with Google Sheet and Script. Every time a user send a survey through Google Form, the answers were saved to Google Sheet. How do I check if there's added data using Google Script? Because once it detected a new answer I want to get the email address on that answer and do a get request to my site's API.

function myFunction() {
  var response = UrlFetchApp.fetch("http://example.com/api/" + emailFromSheet);
  Logger.log(response.getContentText());
}

Upvotes: 0

Views: 716

Answers (1)

ra89fi
ra89fi

Reputation: 1245

There are triggers to do stuff when something happens in a spreadsheet/sheet. onFormSubmit is such a trigger. You can use it to do stuff when form is submitted.

Read more about this installable trigger here and use below code to install the trigger. Read Managing triggers manually section and use form submit event for your needs.

Paste this code then put email column index as specified in code. Save.

Try running the function from script editor by Run > Function > onFormSubmit

It'll ask for authorization, accept. This run will show an error such as e is not defined but don't panic. Test code by submitting the form.

function onFormSubmit(e) {
  // get email from last submitted values
  // put correct col index here in place of 0
  // col A = 0, col B = 1 and so on ...
  var emailFromSheet = e.values[0];

  var response = UrlFetchApp.fetch('http://example.com/api/' + emailFromSheet);
  Logger.log(response.getContentText());
}

Upvotes: 2

Related Questions