Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Google App Script to insert data to a google spreadsheet via POST or GET - updated version as per
Copyright 2011 Martin Hawksey
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.
// Usage
// 1. Enter sheet name where data is to be written below
var SHEET_NAME = "Sheet1";
// 2. Run > setup
// 3. Publish > Deploy as web app
// - enter Project Version name and click 'Save New Version'
// - set security level and enable service (most likely execute as 'me' and access 'anyone, even anonymously)
// 4. Copy the 'Current web app URL' and post this in your form/script action
// 5. Insert column names on your destination sheet matching the parameter names of the data you are passing in (exactly matching case)
var SCRIPT_PROP = PropertiesService.getScriptProperties(); // new property service
// If you don't want to expose either GET or POST methods you can comment out the appropriate function
function doGet(e){
return handleResponse(e);
function doPost(e){
return handleResponse(e);
function handleResponse(e) {
// shortly after my original solution Google announced the LockService[1]
// this prevents concurrent access overwritting data
// [1]
// we want a public lock, one that locks for all invocations
var lock = LockService.getPublicLock();
lock.waitLock(30000); // wait 30 seconds before conceding defeat.
try {
// next set where we write the data - you could write to multiple/alternate destinations
var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
var sheet = doc.getSheetByName(SHEET_NAME);
// we'll assume header is in row 1 but you can override with header_row in GET/POST data
var headRow = e.parameter.header_row || 1;
var headers = sheet.getRange(headRow, 1, 1, sheet.getLastColumn()).getValues()[0];
var nextRow = sheet.getLastRow()+1; // get next row
var row = [];
// loop through the header columns
for (i in headers){
if (headers[i] == "Timestamp"){ // special case if you include a 'Timestamp' column
row.push(new Date());
} else { // else use header name to get data
// more efficient to set values as [][] array than individually
sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
// return json success results
return ContentService
.createTextOutput(JSON.stringify({"result":"success", "row": nextRow}))
} catch(e){
// if error return this
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
} finally { //release lock
function setup() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
SCRIPT_PROP.setProperty("key", doc.getId());
Copy link

ownindra commented Aug 18, 2018

how to send data to the Range spreadsheet in ('C2'), in google script?

Copy link

AlexLPD commented Aug 23, 2019

Hi, Im tryng the code, is still runnign on 08 of 2019?
Mine says ok, buy wehen I try to upload some data, the browser send me; cant open site.


Copy link

jdwhite0722 commented Sep 24, 2019

Hi, Is it possible to run a script like this from another script? I have two spreadsheets one that allows lots of users to search data from the master and edit specific fields only the other sheet is the master data sheet which is locked down so only a few can access and update direct. I have a button on the first sheet which i want to allow uses to submit their changes to the data. I currently get an error see attached file which i think is a result of the permissions. from what I can understand i should be able to create a script and publish as a web app then call that from my original button script setting the execution of that web app as executed by me getting round the permissions thing. Is this possible?

Screenshot 2019-09-24 at 09 51 43

Copy link

caracola commented Jun 18, 2020

Hello, I am using the Code to insert data throught webhooks but although I use lock, I lose events because of concurrence. Any idea how to solve It?

Copy link

ignacio-thfoot commented Oct 6, 2020

Great!! do you know how to grant script access to the users that share Edit access to the datasheet?
they can see the sheet and edit it, but no luck with the ajax thingy

Copy link

rajeev2834 commented Jan 15, 2021

Which one is better doPost(e) or to get value from users and submit it to spreadsheet?

Copy link

ewanjames commented Apr 8, 2021

Hi, looking to run very similar script.

Does anyone know if doPost could get round this problem;

  • I'm using Zapier, triggering Google sheets to create a new row is possible
  • However, In Google scripts, you can't use this event to trigger another event.

The only way I can currently do this is with having the data submitted in a Google Form + the 'On form submission' trigger.

Would doPost get round this?

Copy link

Roman-kazi commented May 24, 2021

Custom row number for Header

You didn't use the headRow variable anywhere.
To use custom header update the line 56 with var headers = sheet.getRange(headRow, 1, 1, sheet.getLastColumn()).getValues()[0];

Copy link

mhawksey commented May 24, 2021

@Roman-kazi - thanks, updated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment