【问题标题】:google.script.run.withSuccessHandler does not return valuegoogle.script.run.withSuccessHandler 不返回值
【发布时间】:2019-10-22 03:06:11
【问题描述】:

这让我发疯了,代码昨天还在工作,但现在不行了。我尝试再次检查所有语法,但问题仍然存在。这个来自 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-apps-script google-sheets web-applications


    【解决方案1】:

    我前段时间遇到了这个问题,它也几乎让我发疯(哦,松散类型的 JavaScript 的乐趣!)。问题是您试图将不可接受的类型数据返回到客户端。通过google.script.run 调用的函数对其可以返回的内容有限制(例如,您应该避免使用Date 实例)。

    受限类型

    目前无法返回(查看documentation限制的详细说明):

    1. Date 个实例;
    2. 任何Function;
    3. DOM 元素(尽管<form> 是允许的);

    解决方案

    ary[1]= new Date(r[0]); 更改为ary[1] = r[0]; 应该可以解决问题,只需将Date 解析移至客户端即可。

    【讨论】:

    • @kb 我很高兴 - 在较早的帖子上看到这个总是很高兴:) 这是 Apps Script 中客户端-服务器通信的一个非常微妙的限制,直到今天我不知道什么是这背后的原因。 runner 函数至少可以尝试将值字符串化。它还与我们在日常使用正常形式中处理日期、一般对象等的方式相关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    相关资源
    最近更新 更多