[英]How to copy specific columns of data from one sheet to another
I have a copySheet script that copies data from one google sheet file to another.我有一个 copySheet 脚本,可以将数据从一个 google sheet 文件复制到另一个。 I use this for lookups in the destination file.
我用它来查找目标文件。 It works great but I want to make some improvements.
它工作得很好,但我想做出一些改进。
I've tried playing around with the script to do this myself for both points but my knoweldge is limited and I haven't been able to get it working.我已经尝试过使用脚本来为这两点自己做这件事,但我的知识有限,我无法让它工作。
Here are some example files:以下是一些示例文件:
Example - destination (script) 示例 - 目的地(脚本)
This is the script:这是脚本:
function copySheet()
{
// Opens the spreadsheet where the data is stored
var sourceTable = SpreadsheetApp.openById("1sp8XFuOAGEfakS_td0xEiX3birz5ckoHNKvzrLPGTOI"); // Source file
var srcSheet = sourceTable.getSheetByName("Sheet1");
// Inserts the data into the target file
var targetTable = SpreadsheetApp.openById("1i77LO4W2paMraipyC_bgx09P7TAuL2cwEVCNxGnbjhs"); // Target file
var tarSheet = targetTable.getSheetByName("Sheet1");
// Gets the last row in the source file
var lastRowSource = srcSheet.getLastRow();
var lastCol = "E";
// Reads the source into an array
var aSrc = srcSheet.getRange("A1:" + lastCol + lastRowSource).getValues();
// Save src array to destination
tarSheet.getRange("A1:" + lastCol + lastRowSource).setValues(aSrc);
}
You can get the last column with content by using srcSheet.getLastColumn() .您可以使用srcSheet.getLastColumn()获取最后一列的内容。 Beware, this will retrieve and index (a number) and not a letter, but you don't need to use
A1 notation
when calling getRange , so you can use this number to indicate which columns to get.请注意,这将检索和索引(数字)而不是字母,但是在调用getRange时不需要使用
A1 notation
,因此您可以使用此数字来指示要获取哪些列。
Considering what you want to do, though, I don't think this is useful to achieve your purpose.但是,考虑到您想做什么,我认为这对实现您的目的没有用。
In effect, you want to copy specifically columns A, D and E to your destination sheet, at columns A, B, C.实际上,您希望将 A、D 和 E 列专门复制到目标工作表的 A、B、C 列。
I reworked your code a bit so that it does what you want:我对您的代码进行了一些修改,以便它可以满足您的要求:
function copySheet() {
// Opens the spreadsheet where the data is stored
var sourceTable = SpreadsheetApp.openById("1sp8XFuOAGEfakS_td0xEiX3birz5ckoHNKvzrLPGTOI"); // Source file
var srcSheet = sourceTable.getSheetByName("Sheet1");
// Inserts the data into the target file
var targetTable = SpreadsheetApp.openById("1e6Dqyv14Edwj7JUFjZsM72IOj8Gh4-tMyUV4iGa-x6Q"); // Target file
var tarSheet = targetTable.getSheetByName("Sheet1");
// Gets the last row in the source file
var lastRowSource = srcSheet.getLastRow();
var originColumns = [1, 4, 5]; // Column indexes to be copied (A, D, E)
var destColumns = [1, 2, 3]; // Column indexes to copy to (A, B, C)
// Loop through each column to copy:
for(var i = 0; i < originColumns.length; i++) {
var originColumn = originColumns[i]; // Origin column index
var destColumn = destColumns[i]; // Destination column index
var originRange = srcSheet.getRange(1, originColumn, lastRowSource); // Origin range to copy
var destRange = tarSheet.getRange(1, destColumn, lastRowSource); // Destination range to be copied to
var values = originRange.getValues(); // Getting values from origin column
destRange.setValues(values); // Setting values to destination column
}
}
In this code, the column indexes to be copied are defined in two arrays (variables originColumns
and destColumns
), and then a for
loop is used to iterate through each origin and destination column (copying data from one to the other).在这段代码中,要复制的列索引定义在两个 arrays (变量
originColumns
和destColumns
)中,然后使用for
循环遍历每个源列和目标列(将数据从一个复制到另一个)。 This cannot be done in a more systematic way because the columns to be copied (A, D, E) don't follow a specific pattern.这无法以更系统的方式完成,因为要复制的列(A、D、E)不遵循特定模式。
I hope this is useful to you.我希望这对你有用。
The suggested script works for me but is a bit slow to run.建议的脚本对我有用,但运行起来有点慢。 Because the source file is having values added to it frequently, I have a trigger setup in the destination file to run the script on open.
因为源文件经常添加值,所以我在目标文件中有一个触发器设置,可以在打开时运行脚本。
I'm using this script that is a bit quicker to run:我正在使用这个运行速度更快的脚本:
function copySheet() {
// Opens the spreadsheet where the data is stored
var sourceTable = SpreadsheetApp.openById("1sp8XFuOAGEfakS_td0xEiX3birz5ckoHNKvzrLPGTOI"); // Source file
//Defines source sheet
var srcSheet = sourceTable.getSheetByName("Sheet1");
// Opens the destination spreadsheet
var targetTable = SpreadsheetApp.openById("1i77LO4W2paMraipyC_bgx09P7TAuL2cwEVCNxGnbjhs"); // Target file
//Defines destination target sheet
var tarSheet = targetTable.getSheetByName("Sheet1");
//Retrieves all values from source
var srcValues = srcSheet.getDataRange().getValues();
//For each row in source values,creates new array containing columns A(0), D(3) & E(4) inside of trimedValues array
var trimmedValues = srcValues.map(function(row) {
return[row[0],row[3],row[4]];
})
// Save trimmedValues array to destination
tarSheet.getRange(1, 1, trimmedValues.length, 3).setValues(trimmedValues);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.