【发布时间】: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