简体   繁体   English

解析来自 Google Sheet Web App 的字符串化 JSON

[英]Parsing a stringified JSON coming from a Google Sheet Web App

I'm trying to parse a stringified JSON output from a web app created from a google sheets script.我正在尝试解析从 Google Sheets 脚本创建的网络应用程序的字符串化 JSON 输出。 I thought it couldn't be that complicated, but I've tried everything I could think of or find out online... so now asking for help if that's OK!我认为它不可能那么复杂,但我已经尝试了所有我能想到的或在网上找到的东西......所以现在寻求帮助,如果可以的话!

on the web app / Google Sheets side, the code is:在网络应用程序/谷歌表格方面,代码是:

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);
}

I've published the script, the web app works, I'm OK with that bit.我已经发布了脚本,Web 应用程序可以运行,我对此没有意见。

I've put random values on the spreadsheet: [[1,2],[3,4]] if we speak in matrix format.我在电子表格上放了随机值:[[1,2],[3,4]] 如果我们用矩阵格式说话。

on the other end, I've tried a bunch of stuff including .fetch, JSON.parse() to get the data in a usable format within the Google Sites embedded code, but the real issue is that I think I can't get to allocate the payload to a variable?另一方面,我尝试了很多东西,包括 .fetch、JSON.parse() 以在 Google Sites 嵌入代码中以可用格式获取数据,但真正的问题是我认为我无法获得将有效载荷分配给变量?

I'm using Google Sites to fetch the data.我正在使用 Google 协作平台来获取数据。 with the basic module "<> embed", with the "by URL" option, with the following code:使用基本模块“<> embed”,使用“by URL”选项,使用以下代码:

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

I get the following output - that looks what it should be:我得到以下输出 - 看起来应该是这样:

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

but when trying to include this in a script module ("embed code") - no chance!但是当试图将它包含在脚本模块(“嵌入代码”)中时 - 没有机会!

<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>

I'm pretty sure I'm missing something obvious - but now I ran out of ideas!我很确定我遗漏了一些明显的东西 - 但现在我没有想法了!

Thanks for any help :)谢谢你的帮助 :)

I believe your goal as follows.我相信你的目标如下。

  • In your situation, the bottom script is embedded to the Google site.在您的情况下,底部脚本嵌入到 Google 站点中。
  • You want to retrieve the values from doGet and want to put the value of cell "B2" to the input tag.您想从doGet检索值并将单元格“B2”的值放入输入标记。
  • The settings of Web Apps is Execute the app as: Me and Who has access to the app: Anyone, even Anonymous . Web Apps 的设置是Execute the app as: Me and Who has access to the app: Anyone, even Anonymous

Modification points:改装要点:

  • In your case, I think that return ContentService.createTextOutput(output);在你的情况下,我认为return ContentService.createTextOutput(output); is suitable instead of return HtmlService.createHtmlOutput(output);适合代替return HtmlService.createHtmlOutput(output); in Google Apps Script.在 Google Apps 脚本中。
  • In order to retrieve the values from doGet , in this modification, fetch is used.为了从doGet检索值,在此修改中,使用了fetch
  • You want to retrieve the cell "B2" from usableVariable[1,1];您想从usableVariable[1,1];检索单元格“B2” usableVariable[1,1]; , please modify it to usableVariable[1][1]; , 请修改为usableVariable[1][1];

When above points are reflected to your script, it becomes as follows.当以上几点反映到你的脚本中时,它变成如下。

Modified script:修改后的脚本:

Google Apps Script side: 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 side: 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>

Note:笔记:

  • When you modified the Google Apps Script of Web Apps, please redeploy the Web Apps as new version.当您修改 Web Apps 的 Google Apps Script 时,请将 Web Apps 重新部署为新版本。 By this, the latest script is reflected to the Web Apps.这样,最新的脚本就会反映到 Web Apps 中。 Please be careful this.请注意这一点。
  • In my environment, I could confirm that above HTML & Javascript worked in the Google site by embedding.在我的环境中,我可以通过嵌入来确认上述 HTML 和 Javascript 在 Google 站点中工作。

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM