满足条件时将行复制到另一个工作表

时间:2018-09-27 07:05:50

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

当在行L的单元格L中输入特定条件时,我希望将sheet1(main)中的行从A列复制到K,如果L列包含“ up”,则应复制到下一个可用行sheet2(Sub)。如果将sheet1(Main)上的L列从“ up”更改为“ down”,则应将其从Sheet2(Sub)中删除并复制到Sheet3(Done)中,反之亦然。

function copyrange() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName('Main'); //source sheet
    var testrange = sheet.getRange('L:L');
    var testvalue = (testrange.getValues());
    var csh = ss.getSheetByName('Sub'); //destination sheet
    var sch = ss.getSheetByName('Done'); //destination sheet
    var data = [];
    var j =[];


    //Condition check in L:L; If true copy the same row to data array
    for (i=0; i<testvalue.length;i++) {
    if ( testvalue[i] === 'up') // changed to ===, == is used to check if they are the same object(have same memory location).
    {
        data.push.apply(data,sheet.getRange(i+1,1,1,11).getValues());

        // Copy matched ROW numbers to j
        j.push(i);
        csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length)
           .setValues(data);
    }
    else ( testvalue[i] === 'down') // changed to ===, ditto.
    {
        data.push.apply(data,sheet.getRange(i+1,1,1,11).getValues());

        //Copy matched ROW numbers to j
        j.push(i);
        sch.getRange(sch.getLastRow()+1,1,data.length,data[0].length)
           .setValues(data);

        }
    }
}

0 个答案:

没有答案