Last active
February 4, 2018 13:50
-
-
Save cpq/6ec1c00379b28551a96bafe14c44315d to your computer and use it in GitHub Desktop.
Google spreadsheet script for data uploading
This file contains 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 characters
// Example data upload: | |
// curl -L -H 'Content-Type: application/json' -d '{"id":"sheet1","names":["timestamp","ram"],"points":[[1,2],[3,4]]}' THIS_SCRIPT_URL | |
function doPost(e) { | |
try { | |
var obj = JSON.parse(e.postData.contents); | |
var doc = SpreadsheetApp.getActiveSpreadsheet(); | |
var name = obj.id || 'DATA'; | |
var sheet = doc.getSheetByName(name); | |
if (!sheet) { | |
doc.insertSheet(name); | |
sheet = doc.getSheetByName(name); | |
range = sheet.getRange("H1:Z1"); | |
sheet.deleteColumns(8, range.getNumColumns()); | |
if (obj.names) sheet.appendRow(obj.names); | |
var chart = sheet.newChart() | |
.addRange(sheet.getRange('a1:b9999')) | |
.setChartType(Charts.ChartType.AREA) | |
.setOption('useFirstColumnAsDomain', true) | |
.setPosition(1, 3, 400, 200) | |
.build(); | |
sheet.insertChart(chart); | |
} | |
var points = obj.points || []; | |
for (var i = 0; i < points.length; i++) { | |
sheet.appendRow(points[i]); | |
} | |
return ContentService // return json success results | |
.createTextOutput(JSON.stringify({"result": "success", obj: obj})) | |
.setMimeType(ContentService.MimeType.JSON); | |
} catch (error) { // if error return this | |
return ContentService | |
.createTextOutput(JSON.stringify({"result": "error", "error": error})) | |
.setMimeType(ContentService.MimeType.JSON); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment