简体   繁体   English

如果单元格值 = x,则有效地将行复制并粘贴到另一个工作表

[英]Efficiently Copy and Paste Row to Another Sheet if Cell Value = x

I'm in the planning phase for a piece of code that I am implementing.我正处于我正在实施的一段代码的规划阶段。 I wanted to know what is the most efficient way to copy and paste a row of values to another tab if the value in column "x" equals a certain value.如果“x”列中的值等于某个值,我想知道将一行值复制并粘贴到另一个选项卡的最有效方法是什么。

Here are the methods I'm thinking of.以下是我想到的方法。 I just want to know which is the most efficient as far as speed of execution and # of lines of code.我只想知道就执行速度和代码行数而言哪个最有效。

  • Filter sheet and copy results to new tab.过滤表并将结果复制到新选项卡。
  • Loop through each row and copy them over to new tab if value in cell equals what we are looking for.如果单元格中的值等于我们要查找的值,则遍历每一行并将它们复制到新选项卡。
  • Loop through each row and push to array if value in cell equals what we are looking for.如果单元格中的值等于我们要查找的值,则遍历每一行并推送到数组。 Then paste array values to new tab.然后将数组值粘贴到新选项卡。

Any other methods I missed?我错过了其他任何方法吗?

Example:例子:

在此处输入图片说明

In the above I'd like to copy over the rows with values "xyz" in column D to a new tab.在上面,我想将 D 列中值为“xyz”的行复制到新选项卡。

Edit: I'm going through a minimum of 8K lines on a spreadsheet so speed of execution is key.编辑:我在电子表格上至少要查看 8K 行,因此执行速度是关键。

Copy from Sheet3 and Paste in Sheet2 all the rows excluding those rows where HDR10 is Even and HDR1 is Odd and HDR6 is between 50 and 80从 Sheet3 复制并粘贴到 Sheet2 中的所有行,不包括 HDR10 为偶数、HDR1 为奇数且 HDR6 介于 50 和 80 之间的行

function runOne() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Sheet3');
  var dsh=ss.getSheetByName('Sheet2');
  if(dsh.getLastRow()>1) {
    dsh.getRange(2,1,dsh.getLastRow()-1,dsh.getLastColumn()).clearContent();
  }
  var rg=sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn());
  rg.setBackground('#ffffff');//sheet3 background white
  var vA=rg.getValues();
  var hA=sh.getRange(1,1,1,sh.getLastColumn()).getValues()[0];//get sheet3 headers labels
  var idx={};
  hA.forEach(function(h,i){idx[h]=i;});//relates header name to array index
  var d=0;
  for(var i=0;i-d<vA.length;i++) {
    //delete row if HDR10 is even and HDR1 is odd and HDR6 is between 50 and 80 non inclusive
    Logger.log('i:%s , d: %s, HDR10: %s, HDR1: %s, HDR6: %s',i,d,vA[i-d][idx['HDR10']],vA[i-d][idx['HDR1']],vA[i-d][idx['HDR6']]);
    if(Number(vA[i-d][idx['HDR10']])%2==0 && Number(vA[i-d][idx['HDR1']])%2==1 && Number(vA[i-d][idx['HDR6']])>50 && Number(vA[i-d][idx['HDR6']])<80) {
      vA.splice(i-d++,1);//filter out the ones we do not want
      sh.getRange(i+2,1,1,sh.getLastColumn()).setBackground('#ffff00');//highlight the copied rows
    }
  } 
  dsh.getRange(dsh.getLastRow()+1,1,vA.length,vA[0].length).setValues(vA);
}

Sheet3 Sheet3

在此处输入图片说明

Sheet2表 2

在此处输入图片说明

Although copying and pasting is fast sometimes the job of extracting only the rows you want may slow down the process considerably.尽管复制和粘贴速度很快,但有时仅提取您想要的行的工作可能会大大减慢该过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从范围复制单元格值并将其粘贴到另一个工作表的单行中 - To copy Cell values from a range and paste it in a single row of another sheet 将单元格值从一张纸复制并粘贴到另一张onEdit - copy and paste cell value from one sheet to another onEdit 需要将行从sheet1复制到sheet2,并将单元格值粘贴到sheet2中具有匹配列值的行上 - Need to copy row from sheet1 to sheet2 and paste the cell values on a row in sheet2 with a matching column value 根据条件和单元格值将行复制到 Google 表格上的另一张表格 - Copy row to another sheet on Google Sheets based on criteria and cell value 复制粘贴到另一张纸 - Copy Paste to another sheet 将每行中的值从一个单元格复制到另一个单元格,作为 Google 工作表中的“值” - copy a value from one cell to another cell per row as “value” in Google sheet 将单元格值复制到另一个工作表以匹配值 - Copy cell value to another sheet for matching value 根据单元格值将最后一行从一个工作表复制到另一个工作表,并在主工作表中不断更新 - Copy last row from one sheet to another depending on cell value with constant updates in master sheet 根据单元格中的值将行复制到新工作表 - Copy a row to new sheet based on value in a cell 从最后一行复制特定的列数据并粘贴到另一个工作表 - Copy specific Columns data from Last Row and paste to another sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM