[英]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.我相信你的目标如下。
doGet
and want to put the value of cell "B2" to the input tag.doGet
检索值并将单元格“B2”的值放入输入标记。Execute the app as: Me
and Who has access to the app: Anyone, even Anonymous
. Execute the app as: Me
and Who has access to the app: Anyone, even Anonymous
。return ContentService.createTextOutput(output);
return ContentService.createTextOutput(output);
is suitable instead of return HtmlService.createHtmlOutput(output);
return HtmlService.createHtmlOutput(output);
in Google Apps Script.doGet
, in this modification, fetch
is used.doGet
检索值,在此修改中,使用了fetch
。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.当以上几点反映到你的脚本中时,它变成如下。
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);
}
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.