本文介绍了Google Apps脚本-如果表中的值满足条件,则从表中复制行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to copy entire row(s) into another sheet if A(i) equals the content of a cell (see criteria).

示例:

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

My criteria (which is located in ws2.A4) = "good"

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

Copy row 7 into ws1.A5 as ws1.A7 = "good"

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

Copy row 8 into ws1.A6 as ws1.A8 = "good"

但其他行则不行.

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

(Note: I am trying to adapt this vba code into GAS https://stackoverflow.com/a/12185026/457557)

这是我现在封锁的地方:

Here is where I blocked now :

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

  }
 }

}

推荐答案

类似的东西可以满足您的需求...不确定我是否完全理解您想要的东西...

something like that does what you want... not sure I understood exactly what you wanted though...

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);
}

这篇关于Google Apps脚本-如果表中的值满足条件,则从表中复制行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 07:18