从数组创建新数组并将值推入Apps脚本中的不同工作表的最佳方法是什么?

时间:2018-09-19 17:23:46

标签: arrays google-apps-script google-sheets

Apps Script新手,因此,您能提供的任何帮助/指导将不胜感激!

我在Apps Script中有一个现有的Google Sheets,它在行上进行迭代,并将每行分别推到新的工作表上。我正在尝试对其进行修改,以获取整个阵列,并立即对其进行推送,以使其效率更高,因为它的利用率越来越高。

这是我要处理的数据的示例: enter image description here

使用这么多的代码,我可以成功地过滤出仅包含值的行,对其进行迭代,并确保所有必需的值都已填写。

function userSubmitTest() {
var ss = SpreadsheetApp.openById('1ozOeBPLzouZTEm0SECW0JZwfxHMcr3dr-augiTra6tY');
var dataSheet = ss.getSheetByName('Data Entry');
var downloadSheet = ss.getSheetByName('ExportForDownload');
var historicalSheet = ss.getSheetByName('ExportHistorical');
var spreadsheetTimeZone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
var submittedDate = Utilities.formatDate(new Date(), spreadsheetTimeZone, "MM/dd/yyyy' 'HH:mm:ss");

// Data Sheet Details
var downloadNumColumns = downloadSheet.getLastColumn();
var downloadIndex = downloadSheet.getLastRow();

//Historical Sheet Details
var historicalNumColumns = historicalSheet.getLastColumn();
var historicalIndex = historicalSheet.getLastRow();

// Initial Sheet Values
var newLastColumn = dataSheet.getLastColumn();
var newLastRow = dataSheet.getLastRow();
var dataToProcess = dataSheet.getRange(2, 1, newLastRow - 1, newLastColumn).getValues().filter(function(row) {
    return row[0]
})

// Get Spreadsheet UI
var ui = SpreadsheetApp.getUi();
var dLen = dataToProcess.length;
var row = 2;

// Loop through processing lines to make sure A:D are filled out for each row
for (var i = 0; i < dLen; ++i) {
    var bolCheck = dataToProcess[i][0];
    var sapCheck = dataToProcess[i][1];
    var notesCheck = dataToProcess[i][2];
    var reasonCheck = dataToProcess[i][3];

    if (bolCheck == "") { // Check all processing lines to make sure delivery docs are entered
        var noBOL = ui.alert(
            'Warning!',
            'Please ensure all lines have a Delivery Document number in A2:A, or clear them, before submitting.',
            ui.ButtonSet.OK);
        return
    } else if (sapCheck == "") { // Check all processing lines to make sure order numbers are entered
        var noSAP = ui.alert(
            'Warning!',
            'Please ensure all lines have a SAP/ECC Order Number number in B2:B, or clear them, before submitting.',
            ui.ButtonSet.OK);
        return
    } else if (notesCheck == "") { // Check all processing lines to make sure notes are entered
        var noNotes = ui.alert(
            'Warning!',
            'Please ensure all lines have notes listed in C2:C, or clear them before submitting.',
            ui.ButtonSet.OK);
        return
    } else if (reasonCheck == "") { // Check all processing lines to make sure reasons are entered
        var noNotes = ui.alert(
            'Warning!',
            'Please ensure all lines have a reason selected in D2:D, or clear them before submitting.',
            ui.ButtonSet.OK);
        return
    }

}

我可以说很多工作正常,因为在调试中,我可以看到以下2D数组: enter image description here

但是,我需要使用该数组,并在顺序位置之间插入一些值,并在下载过程的末尾添加一些新值。在行上进行迭代时,这非常容易,因为我基本上会说“对于该行,这个值是什么?”,然后为我要推送的所有值创建一个新的行“数组”。但是,如何将A:A列中的所有值放入一行,然后根据这些顺序位置和其他要插入的文本/日期开始构建新数组?构建数组后,请一次设置所有值,而不是逐行进行迭代。

这是我当前遍历行时使用的内容:

for (var j = 0; j < dLen; ++j) {
    var bolCheck = dataToProcess[j][0];
    var sapCheck = dataToProcess[j][1];
    var notesCheck = dataToProcess[j][2];
    var reasonCheck = dataToProcess[j][3];
    var rdd = dataToProcess[j][4];
    if (rdd) {
        rdd = Utilities.formatDate(rdd, spreadsheetTimeZone, "MM/dd/yyyy");
    }
    var submittedBy = dataToProcess[j][5].toString();
    var submittedOn = dataToProcess[j][6];
    if (submittedOn) {
        submittedOn = Utilities.formatDate(submittedOn, spreadsheetTimeZone, "MM/dd/yyyy' 'HH:mm:ss");
    }
    var array = [];
    array.push(bolCheck, "Company Submitted", sapCheck, reasonCheck, rdd, "Company Order Desk", "1-800-999-9999", "", notesCheck, submittedBy, submittedBy, submittedOn, submittedDate, "Company Submitted " + submittedDate, "Company Submitted " + submittedDate, submittedDate);

        downloadSheet.insertRowsAfter(downloadIndex, 1).getRange(downloadIndex + 1, 1, 1, historicalNumColumns).setValues([array]);
        historicalSheet.insertRowsAfter(historicalIndex, 1).getRange(historicalIndex + 1, 1, 1, historicalNumColumns).setValues([array]);

如果其中任何一个不清楚,请务必让我知道,以便添加更多上下文。知道链接的任何人都可以编辑Google表格,因此您应该看不到我试图尝试的内容。

感谢您的光临!

1 个答案:

答案 0 :(得分:2)

我认为您几乎已经弄明白了。最后要做的是在最后一个函数中将单行数组添加到另一个“所有行”数组中。像这样:

var allRows = []; //define outside your loop
for (var j = 0; j < dLen; ++j) {
    var bolCheck = dataToProcess[j][0];

    //... same code you already have ...

    array.push(bolCheck, "Company Submitted", sapCheck, reasonCheck, rdd, "Company Order Desk", "1-800-999-9999", "", notesCheck, submittedBy, submittedBy, submittedOn, submittedDate, "Company Submitted " + submittedDate, "Company Submitted " + submittedDate, submittedDate);

    allRows.push(array);
} //close your for loop

downloadSheet
    .insertRowsAfter(downloadIndex, allRows.length)
    .getRange(downloadIndex + 1, 1, allRows.length, historicalNumColumns)
    .setValues(allRows);
historicalSheet
    .insertRowsAfter(historicalIndex, allRows.length)
    .getRange(historicalIndex + 1, 1, allRows.legnth, historicalNumColumns)
    .setValues(allRows);

ps:由于此代码段不完整,因此我没有对此进行测试