-
-
Save yihyang/5e46fc9759741f19a06a19c8e00278a3 to your computer and use it in GitHub Desktop.
Revisions
-
nyancodeid revised this gist
Jul 17, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ # Google Script CRUD By Ryan Aunur Rassyid Simply create RESTful API with Google Script and store it to Google SpreadSheet like a Pro. Learn From http://www.androidlabs.info/appscript/crud-operation-on-google-spread-sheet-using-google-app-script-html-jquery/ -
nyancodeid revised this gist
Aug 4, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -101,7 +101,7 @@ function doUpdate(req, sheet) var lr = sheet.getLastRow(); var headers = _getHeaderRow(sheet); var updatesHeader = Object.keys(updates); // Looping for row -
nyancodeid revised this gist
Aug 7, 2017 . 1 changed file with 107 additions and 20 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,16 +6,27 @@ function doGet(req) { var db = SpreadsheetApp.openById("YOUR SPREADSHEET ID"); // Don't forget to change your Sheet Name by default is 'Sheet1' var sheetUsers = db.getSheetByName("Users"); switch(action) { case "read": return doRead(req, sheetUsers); break; case "insert": return doInsert(req, sheetUsers); break; case "update": return doUpdate(req, sheetUsers); break; case "delete": return doDelete(req, sheetUsers); break; default: return response().json({ status: false, message: 'silent!' }); } } @@ -25,19 +36,57 @@ function doGet(req) { * @request-parameter | action<string> * @example-request | ?action=read */ function doRead(request, sheetObject) { var data = {}; data.records = _readData(sheetObject); return response().json(data); } /* Insert * */ function doInsert(req, sheet) { var id = req.parameter.id; var username = req.parameter.username; var email = req.parameter.email; // all data your needed var flag = 1; var Row = sheet.getLastRow(); for (var i = 1; i <= Row; i++) { /* getRange(i, 2) * i | is a row index * 1 | is a id column index ('id') */ var idTemp = sheet.getRange(i, 1).getValue(); if (idTemp == id) { flag = 0; var result = "Sorry bratha, id already exist"; } } // add new row with recieved parameter from client if (flag == 1) { var timestamp = Date.now(); var currentTime = new Date().toLocaleString(); // Full Datetime var rowData = sheet.appendRow([ id, username, email, timestamp, currentTime ]); var result = "Insertion successful"; } return response().json({ result: result }); } /* Update * request for Update @@ -75,16 +124,45 @@ function doUpdate(req, sheet) } } // Output return response().json({ status: true, message: "Update successfully" }); } /* Delete * */ function doDelete(req, sheet) { var id = req.parameter.id; var flag = 0; var Row = sheet.getLastRow(); for (var i = 1; i <= Row; i++) { var idTemp = sheet.getRange(i, 1).getValue(); if (idTemp == id) { sheet.deleteRow(i); var result = "deleted successfully"; flag = 1; } } if (flag == 0) { return response().json({ status: false, message: "ID not found" }); } return response().json({ status: true, message: result }); } @@ -124,4 +202,13 @@ function _getHeaderRow(sheetObject) { var sh = sheetObject; return sh.getRange(1, 1, 1, sh.getLastColumn()).getValues()[0]; } function response() { return { json: function(data) { return ContentService .createTextOutput(JSON.stringify(data)) .setMimeType(ContentService.MimeType.JSON); } } } -
nyancodeid revised this gist
Aug 7, 2017 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,2 +1,7 @@ # Google Script CRUD By Ryan Aunur Rassyid Simplily create RESTful API with Google Script and store it to Google SpreadSheet like a Pro. Learn From http://www.androidlabs.info/appscript/crud-operation-on-google-spread-sheet-using-google-app-script-html-jquery/ -
nyancodeid revised this gist
Aug 7, 2017 . 2 changed files with 84 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2 @@ # Google Script CRUD By Ryan Aunur Rassyid<[email protected]> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,34 +8,66 @@ function doGet(req) { var sheetUsers = db.getSheetByName("Users"); // Get Route if (action == "read") { return doRead(req, sheetUsers); } // Update Route if (action == "update") { return doUpdate(req, sheetUsers); } } /* Read * request for all Data * * @request-parameter | action<string> * @example-request | ?action=read */ function read_value(request, sheetObject) { var output = ContentService.createTextOutput(), var data = {}; data.records = _readData(sheetObject); output.setContent(JSON.stringify(data)) .setMimeType(ContentService.MimeType.JSON); return output; } /* Update * request for Update * * @request-parameter | id<string>, data<JSON>, action<string> * @example-request | ?action=update&data={"email":"ryandevstudio@gmail.com", "username":"nyancodeid"} */ function doUpdate(req, sheet) { var id = req.parameter.id; var updates = JSON.parse(req.parameter.data); var lr = sheet.getLastRow(); var headers = getHeaderRow_(sheet); var updatesHeader = Object.keys(updates); // Looping for row for (var row = 1; row <= lr; row++) { // Looping for available header / column for (var i = 0; i <= (headers.length - 1); i++) { var header = headers[i]; // Looping for column need to updated for (var update in updatesHeader) { if (updatesHeader[update] == header) { // Get ID for every row var rid = sheet.getRange(row, 1).getValue(); if (rid == id) { // Lets Update sheet.getRange(row, i + 1).setValue(updates[updatesHeader[update]]); } } @@ -45,11 +77,51 @@ function update_value(req, sheet) { result = JSON.stringify({ status: true, message: "Update successfully" }); // Output var output = ContentService.createTextOutput(); return ContentService .createTextOutput(result) .setMimeType(ContentService.MimeType.JSON); } /* Service */ function _readData(sheetObject, properties) { if (typeof properties == "undefined") { properties = _getHeaderRow(sheetObject); properties = properties.map(function (p) { return p.replace(/\s+/g, '_'); }); } var rows = _getDataRows(sheetObject), 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(sheetObject) { var sh = sheetObject; return sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues(); } function _getHeaderRow(sheetObject) { var sh = sheetObject; return sh.getRange(1, 1, 1, sh.getLastColumn()).getValues()[0]; } -
nyancodeid revised this gist
Aug 7, 2017 . No changes.There are no files selected for viewing
-
nyancodeid created this gist
Aug 7, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ /* Route * All Request with Method Get will be proces here */ function doGet(req) { var action = req.parameter.action; var db = SpreadsheetApp.openById("YOUR SPREADSHEET ID"); var sheetUsers = db.getSheetByName("Users"); // Update Route if (action == "update") { return update_value(req, sheetUsers); } } // Update function update_value(req, sheet) { var output = ContentService.createTextOutput(); var id = req.parameter.id; var updates = JSON.parse(req.parameter.data); var lr = sheet.getLastRow(); var headers = getHeaderRow_(sheet); var updatesHeader = Object.keys(updates); for (var row = 1; row <= lr; row++) { for (var i = 0; i <= (headers.length - 1); i++) { var header = headers[i]; for (var update in updatesHeader) { if (updatesHeader[update] == header) { var rid = sheet.getRange(row, 1).getValue(); if (rid == id) { sheet.getRange(row, i + 1).setValue(updates[updatesHeader[update]]); } } } } } result = JSON.stringify({ status: true, message: "Update successfully", log: logH }); return ContentService .createTextOutput(result) .setMimeType(ContentService.MimeType.JSON); }