简体   繁体   English

如何将特定列的数据从一张表复制到另一张表

[英]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.它工作得很好,但我想做出一些改进。

  • Change lastCol to get the last column in the sheet as with the rows.更改 lastCol 以获取工作表中的最后一列,就像行一样。
  • Copy only certain columns from the source (Columns A, D, E) and paste into the destination in order (Columns A, B, C)仅复制源中的某些列(A、D、E 列)并按顺序粘贴到目标(A、B、C 列)

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 - source 示例 - 来源

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 (变量originColumnsdestColumns )中,然后使用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.

相关问题 将数据从一张纸复制到另一张电子表格,但不包括特定的列 - Copy data from one sheet to another spreadsheet exclude specific columns 从最后一行复制特定的列数据并粘贴到另一个工作表 - Copy specific Columns data from Last Row and paste to another sheet 根据特定的键值将数据从一张纸复制到另一张纸 - Copy data from one sheet to another based on a specific key value 谷歌脚本将特定列从一张纸复制到另一张纸上的最后一行 - Google Script to copy specific columns from one sheet to another sheets last row onEdit 将数据从工作表中的特定单元格复制到另一个工作表 - copy data from specific cell in a sheet to another sheet Google Spreadsheet-如何将单元格数据从一张工作表复制到另一张工作表中的单元格? - Google Spreadsheet - How to copy cell data from one sheet to a cell in another sheet? 如何在下一个空白行将数据从一个 Google 表格复制到另一个表格 - How to Copy Data from One Google Sheet to Another Sheet at the next blank row 当特定条件为真时,将值从一张纸复制到另一张纸 - copy values from one sheet to another when a specific condition is true 如何将特定行从一张表复制到另一张表(Google Apps 脚本) - How to copy a specific row from one sheet to another (Google Apps Script) 将特定范围复制到从一个工作表到另一个工作表中的第一个空行和特定列 - Copy a specific range to from one sheet to the first empty row and specific column in another sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM