简体   繁体   English

将单元格值分配给变量失败

[英]Assigning cell values to variables fails

I'm writing a script for a google spreadsheet that should check to see whether the value of a particular cell is "Yes." 我正在为Google电子表格编写脚本,该脚本应检查以查看特定单元格的值是否为“是”。 If so, it should just end. 如果是这样,它应该结束。 If not, it should send an e-mail with the contents of several other cells to a defined address and write "Yes" to the designated cell. 如果没有,它应将包含其他几个单元格内容的电子邮件发送到定义的地址,并向指定的单元格写入“是”。 However, the if condition in the code seems to always evaluate as true even when it should evaluate as false. 但是,代码中的if条件似乎总是评估为true,即使它应该评估为false。 The script writes "Yes" to the designated cell and sends the e-mail. 该脚本将“是”写入指定的单元格并发送电子邮件。 However, while the e-mail contains the contents of the designated cells, it's all out of order. 但是,尽管电子邮件包含指定单元格的内容,但它们都是乱序的。 Here's the script: 这是脚本:

function sendNotification() {

    var sheet = SpreadsheetApp.getActiveSheet();
    var dataRange = sheet.getRange(2,1,1,6);
    var data = dataRange.getValues();
    var dateSubmitted = data[0];
    var dateDue = data[1];
    var url = data[2];
    var currentText = data[3];
    var newText = data[4];
    var emailSent = data[5];
    var test = 'Yes';
    var emailAddress = 'harland.westgate@gmail.com';
    var message = 'On ' + dateSubmitted + ' a colleague requested that ' + url + ' be changed to replace the text ' + currentText + ' with ' + newText + '.  This change should be completed by ' + dateDue;
    var subject = 'A website change has been requested';

    if (emailSent!=test) {  // Prevents sending duplicates
        MailApp.sendEmail(emailAddress, subject, message);
        sheet.getRange(2,6).setValue(test);
        // Make sure the cell is updated right away in case the script is interrupted
        SpreadsheetApp.flush();
   }
}

The relevant cells are (sorry, tried to screenshot but rep is too low): 相关单元格是(很抱歉,尝试截图,但rep太低):

A2: 1/11/14 B2: 1/17/14 C2: www.google.com D2: Lorem ipsum E2: Dolor sit amit F2: Yes A2:2014年1月11日B2:2014年1月17日C2:www.google.com D2:ipre ipsum E2:Dolor座位准许F2:是

here's what it dumps into the body of the e-mail: 这是转储到电子邮件正文中的内容:

On Sat Jan 11 2014 00:00:00 GMT-0500 (EST),Fri Jan 17 2014 00:00:00 GMT-0500 (EST),www.google.com,Lorem ipsum,Dolor sit amit,Yes a colleague requested that undefined be changed to replace the text undefined with undefined. 2014年1月11日星期六00:00:00 GMT-0500(EST),2014年1月17日星期五00:00:00 GMT-0500(EST),www.google.com,Lorem ipsum,Door sit a amit,是的,要求一位同事更改未定义,以将未定义文本替换为未定义。 This change should be completed by undefined 此更改应通过未定义完成

I've searched extensively and have not found an explanation for either behavior. 我进行了广泛的搜索,但未找到任何一种行为的解释。 I don't think it's that the script is failing to read in the values of the cell range, because the e-mail body contains those values (just not where I was expecting them). 我不认为该脚本无法读取单元格区域的值,因为电子邮件正文包含这些值(只是不在我期望的位置)。 Then again, in the places I was expecting those values, it drops in "undefined." 再一次,在我期望这些值的地方,它变为“未定义”。

Remember getValues() returns a two-dimensional array of values. 请记住, getValues()返回一个二维值数组。

Try: 尝试:

...
var dateSubmitted = data[0][0];
var dateDue = data[0][1];
var url = data[0][2];
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM