第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

解析來(lái)自 Google Sheet Web App 的字符串化 JSON

解析來(lái)自 Google Sheet Web App 的字符串化 JSON

慕桂英546537 2023-04-20 10:18:03
我正在嘗試解析從 google 表格腳本創(chuàng)建的網(wǎng)絡(luò)應(yīng)用程序的字符串化 JSON 輸出。我認(rèn)為它不會(huì)那么復(fù)雜,但我已經(jīng)嘗試了我能想到的或在網(wǎng)上找到的所有東西......所以如果可以的話現(xiàn)在尋求幫助!在網(wǎng)絡(luò)應(yīng)用程序/谷歌表格方面,代碼是: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);}我已經(jīng)發(fā)布了腳本,Web 應(yīng)用程序可以運(yùn)行,我對(duì)此很滿意。我在電子表格中放置了隨機(jī)值:[[1,2],[3,4]] 如果我們以矩陣格式說(shuō)話。另一方面,我嘗試了很多東西,包括 .fetch、JSON.parse() 以在 Google 站點(diǎn)嵌入式代碼中以可用格式獲取數(shù)據(jù),但真正的問(wèn)題是我認(rèn)為我無(wú)法獲取將有效負(fù)載分配給變量?我正在使用 Google 協(xié)作平臺(tái)來(lái)獲取數(shù)據(jù)。使用基本模塊“<> embed”,使用“by URL”選項(xiàng),使用以下代碼:https://script.google.com/macros/s/scriptID/exec我得到以下輸出 - 看起來(lái)應(yīng)該是這樣的:{"data":[[1,2],[3,4]]}但是當(dāng)試圖將其包含在腳本模塊(“嵌入代碼”)中時(shí) - 沒(méi)有機(jī)會(huì)!<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>我很確定我錯(cuò)過(guò)了一些明顯的東西 - 但現(xiàn)在我沒(méi)有想法了!謝謝你的幫助 :)
查看完整描述

1 回答

?
胡子哥哥

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊

我相信你的目標(biāo)如下。

  • 在您的情況下,底部腳本嵌入到 Google 站點(diǎn)。

  • 您想要從中檢索值doGet并將單元格“B2”的值放入輸入標(biāo)簽。

  • Web Apps 的設(shè)置是Execute the app as: MeWho has access to the app: Anyone, even Anonymous。

修改點(diǎn):

  • 對(duì)于您的情況,我認(rèn)為這return ContentService.createTextOutput(output);return HtmlService.createHtmlOutput(output);在 Google Apps 腳本中更合適。

  • 為了從中檢索值doGet,在此修改中,fetch使用了。

  • 您要從 中檢索單元格“B2” usableVariable[1,1];,請(qǐng)將其修改為usableVariable[1][1];

當(dāng)以上幾點(diǎn)反映到您的腳本中時(shí),它會(huì)變成如下。

修改腳本:

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>

筆記:

  • 當(dāng)您修改 Web Apps 的 Google Apps Script 時(shí),請(qǐng)將 Web Apps 重新部署為新版本。由此,最新的腳本被反映到Web Apps。請(qǐng)注意這一點(diǎn)。

  • 在我的環(huán)境中,我可以通過(guò)嵌入確認(rèn)上述 HTML 和 Javascript 在 Google 站點(diǎn)中有效。


查看完整回答
反對(duì) 回復(fù) 2023-04-20
  • 1 回答
  • 0 關(guān)注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)