簡體   English   中英

google.script.run.withSuccessHandler 不返回值

[英]google.script.run.withSuccessHandler does not return value

這讓我發瘋,代碼昨天還在工作,但現在不行了。 我嘗試再次檢查所有語法,但問題仍然存在。 這個來自 Google 表格的服務器端請求在服務器端顯示值( Logger.log() ),但在客戶端返回null

function supervisorLine(lineData) {
  if (lineData == 'Name Value is not VALID!') {
    console.log("Supervisor Name Issue!");
  } else {
    document.getElementById('Team').value = lineData[7];
    document.getElementById('Shift').value = lineData[12];
    document.getElementById('action').classList.remove("disabled");
    console.log("team " + lineData[7] + " shift " + lineData[12]);
    ////////////////////////////////// need to be Moved somewhere after password check!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    google.script.run.withSuccessHandler(function(quickStat2) {
      console.log(quickStat2)
    }).loginRecords(lineData[7], lineData[12]);
  }
}

這也是我的服務器端代碼:

function loginRecords(team, shift) {
  var sh = ss.getSheetByName("Attn Rec");
  var result = [];
  var matchRec = sh.getRange("a2:l" + sh.getLastRow()).getValues().filter(function(a) {
    return a[1] === shift && a[4].valueOf() == team.valueOf()
  });
  uniqeLogin = removeDups(matchRec.map(function(a) {
    return a[9]
  }));
  // Logger.log(uniqeLogin);
  uniqeLogin.forEach(function(a) {
    var ary = [team, 0, shift, "", 0, 0, 0, 0, 0];
    matchRec.forEach(function(r) {
      if (r[9] === a) {
        ary[1] = new Date(r[0]);
        ary[3] = r[8];
        switch (r[3].toString().toLowerCase()) {
          case "off-site work":
          case "hr action":
            ary[8]++;
            break;
          case "present":
          case "late transfer":
          case "transfer":
            ary[4]++;
            break;
          case "no show":
            ary[5]++;
            break;
          case "Sick":
          case "vacation":
            ary[7]++;
            break;
          case "late":
          case "approved delay start":
            ary[6]++;
            break;
        }
      }
    });
    result.push(ary);
  });
  Logger.log(result);
  return result;
}

回顧一下, Logger.log(result)返回我需要的數組,但console.log(quickStat2)返回null

前段時間我遇到了這個問題,它也幾乎把我逼瘋了(哦,松散類型 JavaScript 的樂趣!)。 問題是您試圖將不可接受的數據類型返回給客戶端。 通過google.script.run調用的函數對它們可以返回的google.script.run有限制(例如,您應該避免Date實例)。

受限類型

目前,您無法返回(查看文檔以了解限制的詳細說明):

  1. Date實例;
  2. 任何Function
  3. DOM 元素(盡管<form>是允許的);

解決方案

更改ary[1]= new Date(r[0]); ary[1] = r[0]; 應該可以解決問題,只需將Date解析移動到客戶端即可。

對我來說,它與谷歌的黑盒序列化過程有關。 如果該值源自range.getValues()調用或其他 Google API,則它可能不是普通對象/數組/基元,可能存在復雜的底層對象或未通過本機序列化的意外Date

強制執行常規 JS 序列化可避免此問題。

return JSON.parse(JSON.stringify(obj));

完整示例:

function getFirstCellInfo() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DB");
  const values = sheet.getRange(1, 1).getValues();
  const row = values[0];

  const cellInfo = {
    value: row[0],
    valueType: typeof row[0]
  };

  const serializedCellInfo = JSON.parse(JSON.stringify(cellInfo));
  // serializedCellInfo is guaranteed to be a plain JS object
  return serializedCellInfo;
}

暫無
暫無

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

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