如果cell = 0,则循环遍历一列并将数据复制到另一张工作表

时间:2019-06-25 16:35:07

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

我正在尝试遍历特定列并查找值。当单元格值= 0时,我想将该行中的其他列复制到另一张工作表。

function selectCopy() {

var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Intake');
var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Service Pending');
var sheet3 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Service Complete');
var sheet4 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('To Reissue');
var sheet5 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Discovery');

var lastRow1 = sheet1.getLastRow();
var lastRow2 = sheet2.getLastRow();
var lastRow3 = sheet3.getLastRow();
var lastRow4 = sheet4.getLastRow();
var lastRow5 = sheet5.getLastRow();

var startRow = 2;

var range = sheet2.getRange(2,1, lastRow1-startRow+1,1);

var numRows2 = range.getNumRows();
var case_number_values = range.getValues();

range = sheet2.getRange(2,2, lastRow1-startRow+1,1);
var name_values = range.getValues();

var range = sheet2.getRange(2,6, lastRow2-startRow+1,1);

var days_left_values = range.getValues();
var selection = sheet2.getDataRange();

range = sheet2.getRange(2,7, lastRow2-startRow+1,1);
var sent_to_server_values = range.getValues();

range = sheet2.getRange(2, 4, lastRow2-startRow+1,1);
var issue_date_values = range.getValues();

range = sheet2.getRange(2, 5, lastRow2-startRow+1,1);
var service_deadline_values = range.getValues();

range = sheet2.getRange(2,8, lastRow2-startRow+1,1);
var service_date_values = range.getValues();


for (var i=0; i<= numRows2-1; i++){
  var days_left = days_left_values[i][0];
  if (days_left === 0){
    var case_number = case_number_values[i][0];
    var name = plaintiff_name_values[i][0];
    var service_date = service_date_values[i][0];
    var issue_date = issue_date_values[i][0]
    var service_deadline = service_deadline_values[i][0];
    var sent_to_server = sent_to_server_values[i][0];

    sheet4.getRange(lastRow4+1,1).setValue(case_number);
    sheet4.getRange(lastRow4+1,2).setValue(name);
    sheet4.getRange(lastRow4+1,4).setValue(service_deadline);
}
}
}

这是一种工作方式,但是它仅复制具有单元格= 0的最后一行。因此,如果我在列6中有三行,whos值= 0,我想复制所有三行,则此代码仅复制最后一行。

1 个答案:

答案 0 :(得分:0)

这运行得很慢吗?如果重写一点,您将获得很多时间。您实际上只需要读取一次数据,然后每行写入一次。

function selectCopy() {


var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Service Pending');
var sheet4 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('To Reissue');

}
var data = sheet2.getDataRange().getValues();
for (var i=1; i< data.length; i++){
  var days_left = data[i][5];//column 6 is days left
  if (days_left === 0){
    var case_number = data[i][0];//column 1 is case number
    var name = data[i][1];//column 2 is plaintiff names
    var service_date = data[i][7];// column 8
    var issue_date = data[i][3]//column 4
    var service_deadline = data[i][4];//column 5 is deadlines
    var sent_to_server = data[i][6];//column 7

    sheet4.appendRow([case_number,name,"",service_deadline]);
}


}