javascript - 尝试从电子表格中获取值时获取 "undefined"

标签 javascript google-apps-script google-sheets google-sheets-api google-forms

当我尝试从电子表格的单元格中获取值时,出现“未定义”错误。问题是,如果我对不同的单元格执行相同的命令,我将获得该单元格中的值。这 2 个单元之间的唯一区别是产生值(value)的方式。

单元格中正确显示的值是直接从与该电子表格关联的 Google 表单生成的。调用时不显示的值是从我在 Google 表单中创建的脚本生成的。

表单脚本(在表单提交时触发):

// This code will set the edit form url as the value at cell "C2".

function assignEditUrls() {
    var form = FormApp.getActiveForm();
    var ss = SpreadsheetApp.openById("my-spreadsheet-id")
    var sheet = ss.getSheets()[0];

    var urlCol = 3; // column number where URL's should be populated; A = 1, B = 2 etc
    var formResponses = form.getResponses();

    for (var i = 0; i < formResponses.length; i++) {
        var resultUrl = formResponses[i].getEditResponseUrl();
        sheet.getRange(2 + i, urlCol).setValue(resultUrl); 
    } 
    SpreadsheetApp.flush();
 }

表格(已更改为 HTML)

<table>
    <tr> <!-- Row 1 -->
        <td>Timestamp</td> <!-- A1 -->
        <td>Name</td> <!-- B1 -->
        <td>Edit form URL</td> <!-- C1 -->
    </tr>
    <tr> <!-- Row 2 -->
        <td>5/26/2015 14:04:09</td> <!-- A2: this value came from the form submittion-->
        <td>Jones, Donna</td> <!-- B2: this value came from the form submittion-->
        <td>https://docs.google.com/forms/d/1-FeW-mXh_8g/viewform?edit2=2_ABaOh9</td> <!-- C2: this value came from the the script in the form -->
    </tr>
</table>

电子表格中的脚本(在表单提交时触发)

function onFormSubmit(e) {

    // This script will get the values from different cells in the spreadsheet 
    // and will send them into an email.

    var name = e.range.getValues()[0][1]; // This will get the value from cell "B2".
    var editFormURL = e.range.getValues()[0][2]; // This will get the value from cell "C2".

    var email = 'my-email@university.edu';
    var subject = "Here goes the email subject."
    var message = 'This is the body of the email and includes'
                  + 'the value from cell "B2" <b>'
                  + name + '</b>. This value is retrieved correctly.' 
                  + '<br>But the value from cell "C2" <b>'+ editFormURL
                  + '</b> show as "undefined".';

    MailApp.sendEmail(email, subject, message, {htmlBody: message});
}

电子邮件如下所示:

发件人:my-email@university.edu

主题:这是电子邮件主题。

正文:

这是电子邮件的正文,包括来自单元格“B2”Jones, Donna 的值。此值已正确检索。

但是单元格“C2”中的值 undefined 显示为“undefined”。

问题:

我做错了什么?

最佳答案

您很可能遇到了竞争条件。

  1. 用户提交表单。这是我们的重要事件。

  2. 提交表单后,将触发与事件关联的所有触发器。

    • assignEditUrls() 在表单脚本中,以及

    • onFormSubmit() 在电子表格脚本中。

    如果您为此事件设置了其他脚本,它们也会触发。这里的复杂之处在于,所有这些触发器都是独立触发的,或多或少是同时触发的,但没有保证执行顺序。电子表格触发器可能会在表单触发器之前运行!所以这是一个问题。

每个触发器都将以特定于其定义的格式接收事件信息。 (参见 Event Objects。)由于 C2 实际上不是表单提交的一部分,因此它不会出现在电子表格函数接收的事件对象中。这是你的第二个问题,但由于你知道值相对于表单输入的偏移量,你可以使用 range.offset()得到它。

另一个问题与文档和电子表格的共享方式有关;每个单独的脚本调用都会收到它自己的电子表格副本,该副本与其他副本同步......最终。一个脚本对电子表格所做的更改不会立即对所有其他用户可见。这会产生三个问题。

怎么办?

您可以尝试协调两个相关触发器函数的操作。如果它们在同一个脚本中,锁定服务可以提供帮助。

您可以只用一个触发器函数来执行这两个操作。

或者您可以让电子表格功能容忍任何延迟,方法是让它等待 C2 被填充。这个片段可以做到这一点......

...
var editFormURL = null;
var loop = 0;
while (!editFormURL) {
  editFormURL = e.range.offset(0,2).getValue(); // This will get the value from cell "C2".
  if (!editFormURL) {
    // Not ready yet, should we wait?
    if (loop++ < 10) {
      Utilities.sleep(2000); // sleep 2 seconds
    }
    else throw new Error( 'Gave up waiting.' );
  }
}

// If the script gets here, then it has retrieved a value for editFormURL.
...

一个额外的问题:由于您使用的是 getValues(),并且使用复数 s,您正在检索二维信息数组。您没有看到问题,因为当您将这些值视为字符串时,javascript 解释器强制将数组转换为您希望的字符串。但这仍然是一个问题 - 如果您想要单个值,请使用 getValue()

关于javascript - 尝试从电子表格中获取值时获取 "undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30535186/

相关文章:

javascript - 为什么我在使用语音 RSS api 时遇到一些问题

javascript - 在 PythonAnywhere 上托管基于 Tornado 的 Python 应用程序时出现 "Error running WSGI application"

javascript - Google App 脚本连接返回错误 401

javascript - 如何在 Google appScripts 的 HtmlService.SandboxMode.IFRAME 中设置 iframe 属性

javascript - 计算 Google Apps 脚本中两个日期之间的差异

google-apps-script - 无权使用 setFormula Google appS 脚本

javascript - 同时调用 Google Script 的次数过多

google-apps-script - 如何使用 Google Apps 脚本隐藏 Google 电子表格中的图像?

javascript - FooTable 的分页 : a jQuery Plugin for Responsive Data Tables

javascript - jQuery 与prototype.js 冲突: Calender icon is not popping up