解构数组

时间:2017-11-04 20:25:56

标签: javascript google-apps-script

如果行中的“已邮寄”字段中没有“是”,我已经写了一个小脚本来发送电子邮件(Google表格)。它工作正常,但我想用数据行中的格式化文本来增强电子邮件。

我需要帮助将此变量的数据分解为多个变量,以便在电子邮件中使用。

var rangeSelect = auxSheet2.getRange(i, 1, 1, auxSheet2.getLastColumn()).getValues();

日志输出正确,看起来像这样......

[17-11-04 12:49:38:534 PDT] [[Tue Oct 03 00:00:00 GMT-05:00 2017, Captain, America, Engineering, 573-555-5555, me@mycompany.com, Equipment, Yes, We have two old 19" VGA monitors, , Test Submission 1, Yes]]

这是代码:

function moveAfter() {

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var auxSheet2 = ss.getSheetByName("Auxiliary");
    var lastColumn = auxSheet2.getLastColumn();

    for (var i = auxSheet2.getLastRow(); i > 1; i--) {

            var yesMailed = auxSheet2.getRange(i, 12).getValue();
            if (yesMailed != "Yes") {
                    var targetCell = auxSheet2.getRange(i, 12);
                    targetCell.setValue("Yes")

                    var rangeSelect = auxSheet2.getRange(i, 1, 1, auxSheet2.getLastColumn()).getValues();
                    Logger.log(rangeSelect)

                    var recipients = "me@work";
                    var message = 'This is a test ';
                    var subject = 'New item for the Auxiliary Annual Garage Sale';
                    var body = message + '  Visit ' + ss.getUrl() + ' to view the changes';
                    MailApp.sendEmail(recipients, subject, body);
            }
    }
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

getValues方法返回值的二维数组(行,列)。

由于您要拉一行,您将对getValues数组的第一个(也是唯一的)条目感兴趣,然后根据位置对该行的编号索引感兴趣。例如:

var rangeRows = auxSheet2.getRange(i, 1, 1, auxSheet2.getLastColumn()).getValues();
var row = rangeRows[0];
// get a single element
var myEmail = row[5];
// make a string out of a couple of elements of row
var sometext = "My Fave Superhero is " + row[1] + " " + row[2];
// or join the whole row with commas
var joinedRecord = row.join(",");