How to Write Data in Spreadsheet using Google Apps Script

Google Apps Script Tutorial Part 4 – Hello Everyone, This is 4th post in Google Apps Script Tutorial series and this time I am going to show “How to Write Data in Google Spreadsheet“. In the 3rd post of the series, I have shown how to create UI application in Google Apps Script, this part is the extension of that form. After going through the post, you will be able to call a function when button clicks and eventually write data in Google Spreadsheet.

Read Also:

How to Write Data in Spreadsheet

You must have remember the ‘Submit’ button created in the earlier post. First you need to open the Google Spreadsheet in Google Script to work with. To do so, use the following methods:

var ss= SpreadsheetApp.openByUrl(“URL_OF_THE_SPREADSHEET”);

var sh=ss. getSheetByName(“SHEET_NAME”);

Second step to Write Data in Google Spreadsheet is to call a function from submit button. This part is very much similar to event handling in Java AWT. To do so, bind the button event with its handler and pass the method name as parameter to call. See the code below:

var btnhandler=p_app.createServerClickHandler(Method-Name-to-call);

btnhandler.addCallbackElement(panel);

btn1.addClickHandler(btnhandler);

Third and Final Step is to actually write data into Google Spreadsheet. For this you need to use the following code:

var lstrow=sh.getLastRow()+1;

var v_name=sh.getRange(“A”+lstrow);

v_name.setValue(“Value to Write”);

Complete Code to Write Data in Google Spreadsheet

Below is the complete code to Write Data into Google Spreadsheet from Google Apps Scripting.

write data in Google Spreadsheet

Just try it and if you face any problem, comment below:

Leave a Reply