目标:
通过 HTTP post 请求将数据作为 JSON 传递,一次将多行附加到 google 工作表中。
产生问题的步骤:
结果:
抱歉,目前无法打开文件。请检查地址并重试。
注意事项:
应用程序脚本部署为 Web 应用程序,可以访问任何匿名用户。
我尝试过的事情(没有一个有效):
Google Apps 脚本代码:
// Google Apps Script Code
var sheetName = "test log"; // Replace with your sheet name
function doGet(e) {
return ContentService.createTextOutput("GET request processed successfully").setMimeType(ContentService.MimeType.TEXT);
}
function doPost(e) {
// Your code for handling POST requests
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
// Get JSON payload from the request
var jsonData = e.postData.contents;
var data = JSON.parse(jsonData);
// Map incoming data to corresponding headers
var mappedData = {
"Date": data.date || "",
"Energy Consumed (kWh)": data.energyConsumed || "",
"Meter ID": data.slaveID || "",
"Gateway Poll Result": data.gatewayPollResult || "",
"1st Register Value": data.reg1val || "",
"2nd Register Value": data.reg2val || ""
// Add more mappings as needed
};
// Construct the data array with the correct order
var orderedData = orderData(mappedData, ["Date", "Energy Consumed (kWh)", "Meter ID", "Gateway Poll Result", "1st Register Value", "2nd Register Value"]);
// Insert data at the second row (index 1)
sheet.insertRowBefore(2);
sheet.getRange(2, 1, 1, orderedData.length).setValues([orderedData]);
return ContentService.createTextOutput("Data added successfully").setMimeType(ContentService.MimeType.TEXT);
}
// Function to order the data array based on specified headers
function orderData(data, headers) {
var orderedData = [];
headers.forEach(function (header) {
if (data.hasOwnProperty(header)) {
orderedData.push(data[header]);
} else {
orderedData.push(""); // If the header doesn't have corresponding data, insert an empty string
}
});
return orderedData;
}
appsscript.json:
{
"timeZone": "Asia/Kolkata",
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"webapp": {
"executeAs": "USER_DEPLOYING",
"access": "ANYONE_ANONYMOUS"
}
}
卷曲:
curl -X POST
-H 'Content-Type: application/json'
-d '[{"Date":"1705599163","Energy Consumed (kWh)":"0.00","Meter ID":"1","Gateway Poll Result":"0","1st Register Value":"0","2nd Register Value":"0"},{"Date":"1705599163","Energy Consumed (kWh)":"999259.13","Meter ID":"2","Gateway Poll Result":"9","1st Register Value":"18803","2nd Register Value":"62898"},{"Date":"1705599163","Energy Consumed (kWh)":"4603.56","Meter ID":"3","Gateway Poll Result":"9","1st Register Value":"32081","2nd Register Value":"19084"},{"Date":"1705599163","Energy Consumed (kWh)":"4563.96","Meter ID":"4","Gateway Poll Result":"9","1st Register Value":"18416","2nd Register Value":"19083"},{"Date":"1705599163","Energy Consumed (kWh)":"42749.73","Meter ID":"5","Gateway Poll Result":"9","1st Register Value":"18214","2nd Register Value":"64956"},{"Date":"1705599163","Energy Consumed (kWh)":"31587.45","Meter ID":"6","Gateway Poll Result":"9","1st Register Value":"18166","2nd Register Value":"50919"},{"Date":"1705599163","Energy Consumed (kWh)":"39329.72","Meter ID":"7","Gateway Poll Result":"9","1st Register Value":"2031","2nd Register Value":"19478"},{"Date":"1705599163","Energy Consumed (kWh)":"131805.58","Meter ID":"8","Gateway Poll Result":"9","1st Register Value":"26161","2nd Register Value":"19707"},{"Date":"1705599163","Energy Consumed (kWh)":"116166.34","Meter ID":"9","Gateway Poll Result":"9","1st Register Value":"37336","2nd Register Value":"19677"},{"Date":"1705599163","Energy Consumed (kWh)":"136570.23","Meter ID":"10","Gateway Poll Result":"9","1st Register Value":"15960","2nd Register Value":"19714"},{"Date":"1705599163","Energy Consumed (kWh)":"41472.99","Meter ID":"11","Gateway Poll Result":"9","1st Register Value":"13560","2nd Register Value":"19486"},{"Date":"1705599163","Energy Consumed (kWh)":"82634.91","Meter ID":"12","Gateway Poll Result":"9","1st Register Value":"40212","2nd Register Value":"19613"},{"Date":"1705599163","Energy Consumed (kWh)":"85259.05","Meter ID":"13","Gateway Poll Result":"9","1st Register Value":"40550","2nd Register Value":"19618"},{"Date":"1705599163","Energy Consumed (kWh)":"147647.23","Meter ID":"14","Gateway Poll Result":"9","1st Register Value":"52912","2nd Register Value":"19724"}]'
https://script.google.com/macros/s/AKfyxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxp2Q/exec
Google 工作表中的标题:
从示例curl 命令来看,JSON 数据似乎是一个数组。但是,在您的脚本中,
e.postData.contents
不用作数组。
并且,对于curl 命令中的JSON 数据,每个元素的键是
"Date","Energy Consumed (kWh)","Meter ID","Gateway Poll Result","1st Register Value","2nd Register Value"
。但是,在您的脚本中,使用了 date, energyConsumed, slaveID, gatewayPollResult, reg1val, reg2val
键。这些键不包含在您的 JSON 数据中。
另外,你的curl命令需要修改。
从这个情况来看,不幸的是,我不确定哪一个是你的实际情况。因此,在这个答案中,我愿意相信你的curl命令中的JSON数据是实际值。由此,我想提出一个修改后的脚本如下。
当您使用以下 JSON 数据时,
[{"Date":"1705599163","Energy Consumed (kWh)":"0.00","Meter ID":"1","Gateway Poll Result":"0","1st Register Value":"0","2nd Register Value":"0"},{"Date":"1705599163","Energy Consumed (kWh)":"999259.13","Meter ID":"2","Gateway Poll Result":"9","1st Register Value":"18803","2nd Register Value":"62898"},{"Date":"1705599163","Energy Consumed (kWh)":"4603.56","Meter ID":"3","Gateway Poll Result":"9","1st Register Value":"32081","2nd Register Value":"19084"},{"Date":"1705599163","Energy Consumed (kWh)":"4563.96","Meter ID":"4","Gateway Poll Result":"9","1st Register Value":"18416","2nd Register Value":"19083"},{"Date":"1705599163","Energy Consumed (kWh)":"42749.73","Meter ID":"5","Gateway Poll Result":"9","1st Register Value":"18214","2nd Register Value":"64956"},{"Date":"1705599163","Energy Consumed (kWh)":"31587.45","Meter ID":"6","Gateway Poll Result":"9","1st Register Value":"18166","2nd Register Value":"50919"},{"Date":"1705599163","Energy Consumed (kWh)":"39329.72","Meter ID":"7","Gateway Poll Result":"9","1st Register Value":"2031","2nd Register Value":"19478"},{"Date":"1705599163","Energy Consumed (kWh)":"131805.58","Meter ID":"8","Gateway Poll Result":"9","1st Register Value":"26161","2nd Register Value":"19707"},{"Date":"1705599163","Energy Consumed (kWh)":"116166.34","Meter ID":"9","Gateway Poll Result":"9","1st Register Value":"37336","2nd Register Value":"19677"},{"Date":"1705599163","Energy Consumed (kWh)":"136570.23","Meter ID":"10","Gateway Poll Result":"9","1st Register Value":"15960","2nd Register Value":"19714"},{"Date":"1705599163","Energy Consumed (kWh)":"41472.99","Meter ID":"11","Gateway Poll Result":"9","1st Register Value":"13560","2nd Register Value":"19486"},{"Date":"1705599163","Energy Consumed (kWh)":"82634.91","Meter ID":"12","Gateway Poll Result":"9","1st Register Value":"40212","2nd Register Value":"19613"},{"Date":"1705599163","Energy Consumed (kWh)":"85259.05","Meter ID":"13","Gateway Poll Result":"9","1st Register Value":"40550","2nd Register Value":"19618"},{"Date":"1705599163","Energy Consumed (kWh)":"147647.23","Meter ID":"14","Gateway Poll Result":"9","1st Register Value":"52912","2nd Register Value":"19724"}]
您的脚本如下。
function doGet(e) {
return ContentService.createTextOutput("GET request processed successfully").setMimeType(ContentService.MimeType.TEXT);
}
function doPost(e) {
var sheetName = "test log"; // Replace with your sheet name
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var jsonData = e.postData.contents;
var data = JSON.parse(jsonData);
// --- I modified the below script.
var headers = ["Date", "Energy Consumed (kWh)", "Meter ID", "Gateway Poll Result", "1st Register Value", "2nd Register Value"];
var orderedData = [headers, ...data.map(o => headers.map(h => o[h] || null))];
// ---
sheet.insertRowBefore(2);
sheet.getRange(2, 1, orderedData.length, orderedData[0].length).setValues(orderedData);
return ContentService.createTextOutput("Data added successfully").setMimeType(ContentService.MimeType.TEXT);
}
当您修改Web Apps的Google Apps脚本时,请将部署修改为新版本。这样,修改后的脚本就会反映在Web Apps中。请注意这一点。
您可以在我的报告“重新部署 Web 应用程序而不更改新 IDE 的 Web 应用程序的 URL(作者:我)”中查看详细信息。
示例curl命令如下。请替换您的网络应用 URL。
curl -L "https://script.google.com/macros/s/AKfyxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxp2Q/exec" \
-d '[{"Date":"1705599163","Energy Consumed (kWh)":"0.00","Meter ID":"1","Gateway Poll Result":"0","1st Register Value":"0","2nd Register Value":"0"},{"Date":"1705599163","Energy Consumed (kWh)":"999259.13","Meter ID":"2","Gateway Poll Result":"9","1st Register Value":"18803","2nd Register Value":"62898"},{"Date":"1705599163","Energy Consumed (kWh)":"4603.56","Meter ID":"3","Gateway Poll Result":"9","1st Register Value":"32081","2nd Register Value":"19084"},{"Date":"1705599163","Energy Consumed (kWh)":"4563.96","Meter ID":"4","Gateway Poll Result":"9","1st Register Value":"18416","2nd Register Value":"19083"},{"Date":"1705599163","Energy Consumed (kWh)":"42749.73","Meter ID":"5","Gateway Poll Result":"9","1st Register Value":"18214","2nd Register Value":"64956"},{"Date":"1705599163","Energy Consumed (kWh)":"31587.45","Meter ID":"6","Gateway Poll Result":"9","1st Register Value":"18166","2nd Register Value":"50919"},{"Date":"1705599163","Energy Consumed (kWh)":"39329.72","Meter ID":"7","Gateway Poll Result":"9","1st Register Value":"2031","2nd Register Value":"19478"},{"Date":"1705599163","Energy Consumed (kWh)":"131805.58","Meter ID":"8","Gateway Poll Result":"9","1st Register Value":"26161","2nd Register Value":"19707"},{"Date":"1705599163","Energy Consumed (kWh)":"116166.34","Meter ID":"9","Gateway Poll Result":"9","1st Register Value":"37336","2nd Register Value":"19677"},{"Date":"1705599163","Energy Consumed (kWh)":"136570.23","Meter ID":"10","Gateway Poll Result":"9","1st Register Value":"15960","2nd Register Value":"19714"},{"Date":"1705599163","Energy Consumed (kWh)":"41472.99","Meter ID":"11","Gateway Poll Result":"9","1st Register Value":"13560","2nd Register Value":"19486"},{"Date":"1705599163","Energy Consumed (kWh)":"82634.91","Meter ID":"12","Gateway Poll Result":"9","1st Register Value":"40212","2nd Register Value":"19613"},{"Date":"1705599163","Energy Consumed (kWh)":"85259.05","Meter ID":"13","Gateway Poll Result":"9","1st Register Value":"40550","2nd Register Value":"19618"},{"Date":"1705599163","Energy Consumed (kWh)":"147647.23","Meter ID":"14","Gateway Poll Result":"9","1st Register Value":"52912","2nd Register Value":"19724"}]'
当这个curl命令与上面的脚本一起运行时,在电子表格上获得以下结果。