解析来自 Google Sheet Web 应用程序的字符串化 JSON
Parsing a stringified JSON coming from a Google Sheet Web App
我正在尝试解析从 google 工作表脚本创建的 Web 应用程序的字符串化 JSON 输出。我认为它不会那么复杂,但我已经尝试了所有我能想到的或在网上找到的东西......所以现在寻求帮助,如果可以的话!
在网络应用程序/Google 表格端,代码是:
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);
}
我已经发布了脚本,Web 应用程序可以正常工作,我对此很满意。
我已将 运行dom 值放在电子表格中:[[1,2],[3,4]] 如果我们以矩阵格式说话。
另一方面,我尝试了很多东西,包括 .fetch、JSON.parse() 以在 Google 站点嵌入式代码中获取可用格式的数据,但是真正的问题是我想我无法将有效负载分配给变量?
我正在使用 Google 站点来获取数据。
使用基本模块“<> 嵌入”,使用“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>
我很确定我遗漏了一些明显的东西 - 但现在我 运行 没主意了!
感谢您的帮助:)
我相信你的目标如下。
- 在您的情况下,底部脚本嵌入到 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 应用程序的 Google Apps 脚本时,请将 Web 应用程序重新部署为新版本。由此,最新的脚本被反映到Web Apps。请注意这一点。
- 在我的环境中,我可以确认上述 HTML 和 Javascript 通过嵌入在 Google 站点中工作。
参考文献:
我正在尝试解析从 google 工作表脚本创建的 Web 应用程序的字符串化 JSON 输出。我认为它不会那么复杂,但我已经尝试了所有我能想到的或在网上找到的东西......所以现在寻求帮助,如果可以的话!
在网络应用程序/Google 表格端,代码是:
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);
}
我已经发布了脚本,Web 应用程序可以正常工作,我对此很满意。
我已将 运行dom 值放在电子表格中:[[1,2],[3,4]] 如果我们以矩阵格式说话。
另一方面,我尝试了很多东西,包括 .fetch、JSON.parse() 以在 Google 站点嵌入式代码中获取可用格式的数据,但是真正的问题是我想我无法将有效负载分配给变量?
我正在使用 Google 站点来获取数据。 使用基本模块“<> 嵌入”,使用“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>
我很确定我遗漏了一些明显的东西 - 但现在我 运行 没主意了!
感谢您的帮助:)
我相信你的目标如下。
- 在您的情况下,底部脚本嵌入到 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 应用程序的 Google Apps 脚本时,请将 Web 应用程序重新部署为新版本。由此,最新的脚本被反映到Web Apps。请注意这一点。
- 在我的环境中,我可以确认上述 HTML 和 Javascript 通过嵌入在 Google 站点中工作。