无法使用 Google Apps 脚本通过 HTTP post 请求将 json 数据添加到 Google 表格

问题描述 投票:0回答:1

目标:
通过 HTTP post 请求将数据作为 JSON 传递,一次将多行附加到 google 工作表中。

产生问题的步骤:

  1. 在谷歌表格中创建了一个名为“测试日志”的表格。
  2. 编写 Apps 脚本以从 HTTP post 请求接收包含多个记录的 JSON 数据,并将每个记录附加到工作表中的新行。 [在下面查找代码]
  3. 运行 cURL 发出 HTTP post 请求,将 JSON 数据附加到工作表中。 [在下面查找代码]

结果:

  1. Apps 脚本引发以下错误。

抱歉,目前无法打开文件。请检查地址并重试。

  1. 请求完成后,将在工作表中添加一个空行。

注意事项:
应用程序脚本部署为 Web 应用程序,可以访问任何匿名用户。

我尝试过的事情(没有一个有效):

  1. 将应用程序脚本所有者从我自己更改为匿名者。检查问题是否与 google 帐户有关。
  2. 在浏览器中打开一个 ingognito 窗口,然后打开 google 工作表并运行 cURL。
  3. 在post请求中,从JSON数据中删除了多条记录,仅替换为一条记录。
  4. 尝试在 stackoverflow 和其他地方发布类似问题的解决方案。

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 工作表中的标题:

  • 日期
  • 面板
  • 能源
  • 消耗量(kWh)
  • 仪表ID
  • 网关投票结果
  • 第一个寄存器值
  • 第二寄存器值
  • 虚拟 Col 1
  • 虚拟 Col 2
json google-apps-script google-sheets http-post google-account
1个回答
0
投票

修改要点:

  • 从示例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);
}

重要

测试:

示例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命令与上面的脚本一起运行时,在电子表格上获得以下结果。

参考资料:

© www.soinside.com 2019 - 2024. All rights reserved.