【问题标题】:Parsing a stringified JSON coming from a Google Sheet Web App解析来自 Google Sheet Web App 的字符串化 JSON
【发布时间】:2020-10-04 19:53:02
【问题描述】:

我正在尝试解析从 google sheet 脚本创建的 web 应用程序的字符串化 JSON 输出。我认为它不会那么复杂,但我已经尝试了所有我能想到的或在网上找到的......所以现在寻求帮助,如果可以的话!

在网络应用/谷歌表格方面,代码是:

function doGet(e) {
  
  var spreadsheet = SpreadsheetApp.openById('spreadsheetID');
  var worksheet = spreadsheet.getSheetByName('Rankings C/U');
  var output = JSON.stringify({ data: worksheet.getDataRange().getValues() });
  
  return HtmlService.createHtmlOutput(output);
}

我已经发布了脚本,网络应用程序可以正常工作,我可以接受。

我在电子表格中输入了随机值:[[1,2],[3,4]],如果我们以矩阵格式表示的话。

另一方面,我尝试了很多东西,包括 .fetch、JSON.parse() 来在 Google 协作平台嵌入代码中以可用格式获取数据,但真正的问题是我认为我可以'不能将有效负载分配给变量?

我正在使用 Google 协作平台来获取数据。 带有基本模块“ embed”,带有“by URL”选项,代码如下:

https://script.google.com/macros/s/scriptID/exec

我得到以下输出 - 看起来应该是什么:

{"data":[[1,2],[3,4]]}

但是当试图将其包含在脚本模块中时(“嵌入代码”) - 没有机会!

<form name="get-images">
  <input name="test" id="test" value="we'll put the contents of cell A1 here">
</form>

<script>
   const form = document.forms['get-images']
   var usableVariable = JSON.parse("https://script.google.com/macros/s/scriptID/exec"); // here I'm trying to allocate the stringified JSON to a variable
   form.elements['test'].value = usableVariable[1,1]; //allocating the first element of the parsed array  
</script>

我很确定我遗漏了一些明显的东西 - 但现在我已经没有想法了!

感谢您的帮助:)

【问题讨论】:

  • 在你的情况下,我认为return HtmlService.createHtmlOutput(output);需要修改为return ContentService.createTextOutput(output);。但是从var test = https://script.google.com/macros/s/scriptID/exec; 和form.elements['test'].value = 123;,我无法理解你想要做什么。我为此道歉。我可以问你目标的细节吗?
  • 感谢@Tanaike 这么快就恢复了!让我编辑帖子以澄清
  • 我已经尝试了您的建议,但并没有改变结果。即使“按 URL”嵌入代码提供了输出,我仍然无法将字符串分配给脚本中的变量。
  • 感谢您的回复。我看到了你更新的问题。 but when trying to include this in a script 和 I've tried JSON.parse() and UrlFetchApp.fetch() 的脚本是什么?您正在尝试在 HTML 和 Javascript 中使用 Google Apps 脚本?我无法理解if this line doesn't process in the preview window of the embedded code insert, I understand that the previous line (the allocation) is illegal and stops the code execution。所以我仍然无法理解你想要做什么。这是因为我的技术不好。对此我深表歉意。
  • @Tanaike,我只是想在这个 Google 网站中使用存储在 Google 表格中的数据。

标签: javascript google-apps-script google-sheets web-applications


【解决方案1】:

我相信你的目标如下。

  • 在您的情况下,底部脚本嵌入到 Google 网站中。
  • 您想从doGet 中检索值,并希望将单元格“B2”的值放入输入标签。
  • Web Apps 的设置是Execute the app as: Me 和Who has access to the app: Anyone, even Anonymous。

修改点:

  • 就您而言,我认为 return ContentService.createTextOutput(output); 适合代替 Google Apps 脚本中的 return HtmlService.createHtmlOutput(output);。
  • 为了从doGet 中检索值,在此修改中,使用了fetch。
  • 您想从usableVariable[1,1];中检索单元格“B2”,请将其修改为usableVariable[1][1];

当以上几点反映到你的脚本中时,它变成如下。

修改后的脚本:

Google Apps 脚本方面:

function doGet(e) {
  var spreadsheet = SpreadsheetApp.openById('spreadsheetID');
  var worksheet = spreadsheet.getSheetByName('Rankings C/U');
  var output = JSON.stringify({ data: worksheet.getDataRange().getValues() });
  return ContentService.createTextOutput(output);
}

HTML & Javascript 方面:

<form name="get-images">
  <input name="test" id="test" value="we'll put the contents of cell A1 here">
</form>

<script>
  let url = "https://script.google.com/macros/s/###/exec";
  fetch(url)
    .then((res) => res.json())
    .then((res) => {
      const usableVariable = res.data;
      const form = document.forms['get-images'];
      form.elements['test'].value = usableVariable[1][1];  // usableVariable[1][1] is the cell "B2".
    });
</script>

注意:

  • 当您修改 Web Apps 的 Google Apps 脚本时,请将 Web Apps 重新部署为新版本。这样,最新的脚本就会反映到 Web 应用程序中。请注意这一点。
  • 在我的环境中,我可以通过嵌入确认上述 HTML 和 Javascript 在 Google 网站中有效。

参考资料:

【讨论】:

  • 非常感谢,它有效!!!! ``` fetch() ``` 周围的编码对我来说并不明显,我离弄清楚它还有几英里远。希望这个用例也能为其他人服务。再次感谢您的耐心等待!
  • @FanF 感谢您的回复和测试。我很高兴你的问题得到了解决。通过您的配合,我可以正确理解和思考解决方案。也谢谢你。
猜你喜欢
  • 1970-01-01
  • 2018-08-15
  • 1970-01-01
  • 1970-01-01
  • 2016-08-15
  • 2020-12-14
  • 2018-01-27
  • 2012-10-04
  • 1970-01-01
相关资源
最近更新 更多