- To open
New >> More >> Google App Script
- Rename
**A google script file will have this basic function ***
function doGetTest(e){
}
sligtly alter it like
function doGetTest(e){
if (e === undefined)e ={parameters:{}}// for running tests
}
- To get the url variable passed use e.parameter;
eg:
var op = e.parameter.action;
-
Create google Spreadsheet Instance
var SPREADSHEET_URL = 'https://docs.google.com/spreadsheets/d/sasasasasadqw323qsqw2333/edit?usp=sharing'; var ss=SpreadsheetApp.openByUrl(SPREADSHEET_URL); var sheet = ss.getSheetByName("Sheet1"); read_value(e,ss);
-
Write a function to read
function read_value(request,ss){ var output = ContentService.createTextOutput(), data = {}; //Note : here sheet is sheet name , don't get confuse with other operation var sheet="sheet1"; //output.setContent('in read_value(request,ss)');return output; data.records = readData_(ss, sheet); var callback = request.parameters.callback; console.log(JSON.stringify(data)); if (callback === undefined) { output.setContent(JSON.stringify(data)); } else { output.setContent(callback + "(" + JSON.stringify(data) + ")"); } output.setMimeType(ContentService.MimeType.JAVASCRIPT); return output; } function readData_(ss, sheetname, properties) { if (typeof properties == "undefined") { properties = getHeaderRow_(ss, sheetname); properties = properties.map(function(p) { return p.replace(/\s+/g, '_'); }); } var rows = getDataRows_(ss, sheetname), data = []; for (var r = 0, l = rows.length; r < l; r++) { var row = rows[r], record = {}; for (var p in properties) { record[properties[p]] = row[p]; } data.push(record); } return data; } function getDataRows_(ss, sheetname) { var sh = ss.getSheetByName(sheetname); return sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues(); } function getHeaderRow_(ss, sheetname) { var sh = ss.getSheetByName(sheetname); return sh.getRange(1, 1, 1, sh.getLastColumn()).getValues()[0]; }
-
Run
|
|