我需要帮助拆分数组

时间:2018-06-07 17:57:51

标签: javascript arrays spreadsheet

我很难编辑这个脚本。它查看一个选项卡,拉出符合条件的所有行并将它们粘贴到另一个选项卡中。下面的脚本有效,但并不适合我的需求。它粘贴整行,我只需要A,B和C列。理想情况下,我需要将数据分别复制到A列,D列和E列中。谢谢你的帮助!

    function Migrate2() {

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName('OnBoard');
    var targetSheet = ss.getSheetByName('NewHire');
    var val = sheet.getRange('a3:l').getValues();
    var headers = val.shift();
    var arr = [],
        rowsToWriteBack = [];

    rowsToWriteBack.push(headers);
    val.forEach(function (r, i) {
        r[7] == 'COMPLETE'&& r[11] == 'N' ? arr.push(r) : rowsToWriteBack.push(r)});

    if (arr.length > 0) {
        targetSheet.getRange(targetSheet.getLastRow() + 1, 1, arr.length, arr[0].length).setValues(arr);

    }
   }

1 个答案:

答案 0 :(得分:0)

试试这个

function Migrate2() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName('OnBoard');
    var targetSheet = ss.getSheetByName('NewHire');
    var val = sheet.getRange('a3:l').getValues();
    var headers = val.shift();
    var arr = [],
    rowsToWriteBack = [];

    rowsToWriteBack.push(headers);

    val.forEach(function (r, i) {
            r[7] == 'COMPLETE'&& r[11] == 'N' ? 
                arr.push([r[0],r[1],r[2]]) : 
                rowsToWriteBack.push([r[0],r[1],r[2]])
    });

        if (arr.length > 0) {
                            for(var i = 0; i < arr.length; i++) {
                                targetSheet.getRange(targetSheet.getLastRow() + i + 1, 1, 1, 1).setValues(arr[i][0]);
                                targetSheet.getRange(targetSheet.getLastRow() + i + 1, 4, 1, 1).setValues(arr[i][1]);
                                targetSheet.getRange(targetSheet.getLastRow() + i + 1, 5, 1, 1).setValues(arr[i][2]);
                            }
        }
    }
}

难以在没有访问您正在使用的环境的情况下进行测试,但我认为上述情况应该有效。祝你好运!

这是更新版本(希望)修复了这个惊人的问题:

function Migrate2() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName('OnBoard');
    var targetSheet = ss.getSheetByName('NewHire');
    var val = sheet.getRange('a3:l').getValues();
    var headers = val.shift();
    var arr = [],
    rowsToWriteBack = [];

    rowsToWriteBack.push(headers);

    val.forEach(function (r, i) {
            r[7] == 'COMPLETE'&& r[11] == 'N' ? 
            arr.push([r[0],r[1],r[2]]) : 
            rowsToWriteBack.push([r[0],r[1],r[2]])
    });

        if (arr.length > 0) {
            for(var i = 0; i < arr.length; i++) {
                var addToRow = targetSheet.getLastRow() + i + 1;

                targetSheet.getRange(addToRow, 1, 1, 1).setValues(arr[i][0]);
                targetSheet.getRange(addToRow, 4, 1, 1).setValues(arr[i][1]);
                targetSheet.getRange(addToRow, 5, 1, 1).setValues(arr[i][2]);
            }
        }
    }
}

尝试#3以防止跳过行

    function Migrate2() {
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        var sheet = ss.getSheetByName('OnBoard');
        var targetSheet = ss.getSheetByName('NewHire');
            var startingRow = targetSheet.getLastRow() + 1;
        var val = sheet.getRange('a3:l').getValues();
        var headers = val.shift();
        var arr = [],
        rowsToWriteBack = [];

        rowsToWriteBack.push(headers);

        val.forEach(function (r, i) {
                r[7] == 'COMPLETE'&& r[11] == 'N' ? 
                arr.push([r[0],r[1],r[2]]) : 
                rowsToWriteBack.push([r[0],r[1],r[2]])
        });

            if (arr.length > 0) {
                for(var i = 0; i < arr.length; i++) {
                    var addToRow = startingRow + i;

                    targetSheet.getRange(addToRow, 1, 1, 1).setValues(arr[i][0]);
                    targetSheet.getRange(addToRow, 4, 1, 1).setValues(arr[i][1]);
                    targetSheet.getRange(addToRow, 5, 1, 1).setValues(arr[i][2]);
                }
            }
        }
    }