Google Apps脚本 - 如果表中的值符合条件,则从表中复制行

时间:2014-07-30 13:54:49

标签: google-apps-script

如果A(i)等于单元格的内容(请参阅条件),我想将整行复制到另一张表格中。

示例:

我的标准(位于ws2.A4)="好"

将第7行复制到ws1.A5中为ws1.A7 =" good"

将第8行复制到ws1.A6中为ws1.A8 =" good"

但不是其他行。

(注意:我正在尝试将此vba代码改编为GAS https://stackoverflow.com/a/12185026/457557

这是我现在阻止的地方:

function copy_row_s_if_cellAi_equal_X() {

var ws1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("base");
var ws2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ok");
var Alast = ws1.getLastRow()
var criteria = ws2.getRange(4 ,1).Value
var target = ws2.getRange(5 ,1)

for (var i = 3; i < Alast; i++ ) {
   if (ws1.getRange(i ,1) == criteria) { 

      ws1.getRange(i ,1).copyTo(target, {contentsOnly:true}); // copy/paste content only

  }
 }

}

1 个答案:

答案 0 :(得分:1)

类似的东西做你想要的......不确定我完全理解你想要的东西......

function copy_row_s_if_cellAi_equal_X() {

  var ws1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("base");
  var ws2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ok");
  var Alast = ws1.getLastRow();
  var criteria = ws2.getRange(4 ,1).getValue();

  for (var i = 3; i < Alast; i++ ) {
    Logger.log(ws1.getRange(i ,1).getValue()+ ' ==? ' + criteria);
    if (ws1.getRange(i ,1).getValue() == criteria) { 

      ws1.getRange(i ,1,1,ws1.getLastColumn()).copyTo(ws2.getRange(i ,1,1,ws1.getLastColumn()), {contentsOnly:true}); // copy/paste content only

    }
  }
}

编辑:高速版

function copy_row_s_if_cellAi_equal_X() {  
  var ws1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("base");
  var ws2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ok");
  var Alast = ws1.getLastRow();
  var criteria = ws2.getRange(4 ,1).getValue();
  var dataws1 = ws1.getRange(3,1,Alast,ws1.getLastColumn()).getValues();
  var outData = [];
  for (var i in dataws1) {
    if (dataws1[i][0] == criteria) { 
      outData.push(dataws1[i])
    }
  }
  ws2.getRange(ws2.getLastRow(),1,outData.length,outData[0].length).setValues(outData);
}