[英]Copy data from a SpreadSheet to another based on filtered range
I have this script that copy the entire content from source spreadsheet to the target spreadsheet.我有这个脚本可以将整个内容从源电子表格复制到目标电子表格。 But I would to copy only a specific column (Col B) dependent form another column (Col C) with the content "Italy".
但是我只想复制特定列(Col B)从属的另一列(Col C),内容为“Italy”。
function CopyDataToNewFile() {
var sss = SpreadsheetApp.openById('1jtZli...'); // sss = source spreadsheet
var ss = sss.getSheetByName('Sorgente'); // ss = source sheet
//Get full range of data
var SRange = ss.getDataRange();
//get A1 notation identifying the range
var A1Range = SRange.getA1Notation();
//get the data values in range
var SData = SRange.getValues();
var tss = SpreadsheetApp.openById('185I...'); // tss = target spreadsheet
var ts = tss.getSheetByName('Destinazione'); // ts = target sheet
//set the target range to the values of the source data
ts.getRange(A1Range).setValues(SData);
}
I have reached this conclusion.我得出了这个结论。 There is a method in the destination sheet (where there is the data validation from the source sheet) where the data validation display only the value not inserted yet in the destination sheet?
目标工作表中有一种方法(其中有来自源工作表的数据验证),其中数据验证仅显示尚未插入目标工作表中的值?
Try this:尝试这个:
function CopyDataToNewFile() {
var sss = SpreadsheetApp.openById('1jtZli...');
var ss = sss.getSheetByName('Sorgente');
var SRange = ss.getDataRange();
var A1Range = SRange.getA1Notation();
var SData = SRange.getValues();
var B=[];
SData.forEach(function(r){
if(r[2]=='Italy') {
B.push([r[1]]);
}
});
var tss = SpreadsheetApp.openById('185I...');
var ts = tss.getSheetByName('Destinazione');
ts.getRange(1,1,B.length,1).setValues(B);
}
Here's a couple of ways that you can play with.您可以使用以下几种方法。 I think the first one will be much faster.
我认为第一个会快得多。
The first one get's data all at one and writes output data all at one time.第一个一次获取所有数据并一次写入所有输出数据。
function Copy() {
var ss=SpreadsheetApp.openById('source spreadsheet id')
var ssh=ss.getSheetByName('sorg');
var dss=SpreadsheetApp.openById('destination spreadsheet id')
var dsh=dss.getSheetByName('dest')
var srg=ssh.getRange(1,1,ssh.getLastRow(),ssh.getLastColumn());
var svA=srg.getValues();
var oA=[];
svA.forEach(function (r,i) {
if (r[2]=='Italy') {
oA.push([r[1]]);
}
});
dsh.getRange(dsh.getLastRow()+1,1,oA.length,1).setValues(oA);
}
The second one get column B and the column C and compares each row of column C with the string 'Italy' if column C equals "Italy" then in appends that same row of column B so it does a lot more writes and one more read.第二个获取 B 列和 C 列,并将 C 列的每一行与字符串 'Italy' 进行比较,如果 C 列等于“Italy”,则在附加 B 列的同一行,这样它会进行更多的写入和读取.
function Copy() {
var ss=SpreadsheetApp.openById('source spreadsheet id');
var ssh=ss.getSheetByName('sorg');
var dss=SpreadsheetApp.openById('destination spreadsheet id');
var dsh=dss.getSheetByName('dest');
var srgB=ssh.getRange(1,2,ssh.getLastRow());//col B
var srgC=ssh.getRange(1,3,ssh.getLastRow());//col C
var BvA=srgB.getValues();
var CvA=srgC.getValues();
var oA=[];
BvA.forEach(function (r,i) {
if(CvA[i][0]=='Italy') {
dss.appendRow([BvA[i][0]]);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.