簡體   English   中英

如何將 sessionStorage 與 JS 和 Google Apps Script 一起使用?

[英]How use sessionStorage with JS and Google Apps Script?

如果sessionStorage中有可用數據,我試圖避免調用服務器端 function getSuppliersForPoFromSS(orderPo, origin) ,但我不確定我得到了正確的結構:

**/
 * Gets determined supplier from Spreadsheet and builds a select input with options.
 * @param {Object} selecObject
 * @param {String} origin specifying where this is going to be obtained from
 */
function loadSuppliersForPoFromSS(selectObject, origin) {
  orderPo = selectObject.value;
  document.getElementById('selectSupplier').innerHTML = '';
   
  //My shot at it, trying to a 2D array. e.g.: [["Item1", "Item2"]]
  let ar = JSON.parse(sessionStorage.getItem(orderPo));
 
   
  if(!ar){   
  google.script.run.withSuccessHandler(function(ar) {
    let select = document.getElementById('selectSupplier');
    let options = select.getElementsByTagName('option');
    for (let i = options.length; i--;) {
      select.removeChild(options[i]);
    }
    let option = document.createElement("option");
    option.value = "";
    option.text = "";
    select.appendChild(option);
    ar.forEach(function(item, index) {
      let option = document.createElement("option");
      option.value = item.toLowerCase();
      option.text = item;
      select.appendChild(option)
    });
  }).getSuppliersForPoFromSS(orderPo, origin);
 } else {
  //Do I rewrit the part that builds the options with data from sessionStorage?
}

由於 google.script.run 是異步的,這意味着它后面的命令不會等待 google.script.run 完成並返回,您可以創建一個內部 function getAr() ,它將作為 google 成功處理程序的一部分調用.script.run 或如果 sessionStorage 返回值則單獨運行。

function loadSuppliersForPoFromSS(selectObject, origin) {
  orderPo = selectObject.value;
  document.getElementById('selectSupplier').innerHTML = '';
  
  function getAr(ar) {
    let select = document.getElementById('selectSupplier');
    let options = select.getElementsByTagName('option');
    for (let i = options.length; i--;) {
      select.removeChild(options[i]);
    }
    let option = document.createElement("option");
    option.value = "";
    option.text = "";
    select.appendChild(option);
    ar.forEach(function(item, index) {
      let option = document.createElement("option");
      option.value = item.toLowerCase();
      option.text = item;
      select.appendChild(option)
    });
  }

  //My shot at it, trying to a 2D array. e.g.: [["Item1", "Item2"]]
  let ar = JSON.parse(sessionStorage.getItem(orderPo));
   
  if(!ar){   
    google.script.run.withSuccessHandler(function(ar) {
      getAr(ar);
    }).getSuppliersForPoFromSS(orderPo, origin);
  } 
  else {
    getAr(ar);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM