[英]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.我只想知道就执行速度和代码行数而言哪个最有效。
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.