将数据从一张电子表格复制到另一张电子表格
Posted
技术标签:
【中文标题】将数据从一张电子表格复制到另一张电子表格【英文标题】:COPYING DATA FROM ONE SPREADSHEET TO ANOTHER 【发布时间】:2019-08-22 05:12:13 【问题描述】:我有三个源文件 - Alpha、Beta、Kappa。它们具有相同的标题,基本上,我只想每周将它们全部合并到一个文件中。我从 Kappa 开始,它以某种方式工作 - 它创建一个新的电子表格并从 Kappa 文件复制信息并将其作为值粘贴到新创建的电子表格中。
现在我尝试将 Alpha 文件添加到同一个电子表格中,就在我使用 getLastRow 函数从 Kappa 文件复制的信息下方。我收到一条错误消息,说源范围和目标范围必须在同一个电子表格上。
function RawExtractCopy()
var version = 'ver. 4.01'
var ssnew = SpreadsheetApp.create('V5 Raw Extract '+ version ); //create a spreadsheet
var ssnewsheet = ssnew.getSheetByName('Sheet1'); //get the sheet named Sheet1
ssnewsheet.insertRows(1,30000); //inserting more rows
ssnewsheet.setName('V5 Raw Extract'); //rename the sheet name of the newly created spreadsheet
var ssKappa = SpreadsheetApp.getActiveSpreadsheet(); //opens source file
var targetss = ssnew; //define created ss as the target ss
var srcSheetKappa = ssKappa.getSheetByName('KAPPA'); //get source sheet name
var targetSheet = targetss.getSheetByName('V5 Raw Extract'); //defining target sheet
var srcRangeKappa = srcSheetKappa.getRange("A1:AM30000"); //get source data
var destRangeKappa = targetss.getRange("A1"); //define target
var values = srcRangeKappa.getValues(); //line 18 to 21 is just to match source sheet and target sheet
var bGcolors = srcRangeKappa.getBackgrounds();
var colors = srcRangeKappa.getFontColors();
var fontSizes = srcRangeKappa.getFontSizes();
destRangeKappa.setValues(values);
destRangeKappa.setBackgrounds(bGcolors);
destRangeKappa.setFontColors(colors);
destRangeKappa.setFontSizes(fontSizes);
srcRangeKappa.copyTo(destRangeKappa, contentsOnly: true);
var ssAlpha = SpreadsheetApp.openById("1gIs4vCdcGG79poDujz9t8Fq7_BWlaEiMrYUH9DTxVHs").activate();
var srcSheetAlpha = ssAlpha.getSheetByName('V5 ALPHA');
var srcRangeAlpha = srcSheetAlpha.getRange("A2:AM30000");
var destRangeAlpha = targetSheet.getRange(targetSheet.getLastRow()+1,1);
var valuesAlpha = srcRangeAlpha.getValues();
var bGcolorsAlpha = srcRangeAlpha.getBackgrounds();
var colorsAlpha = srcRangeAlpha.getFontColors();
var fontSizesAlpha = srcRangeAlpha.getFontSizes();
destRangeAlpha.setValues(values);
destRangeAlpha.setBackgrounds(bGcolors);
destRangeAlpha.setFontColors(colors);
destRangeAlpha.setFontSizes(fontSizes);
srcRangeKappa.copyTo(destRangeAlpha, contentsOnly: true);
事情,它仍然从 Kappa 复制数据,但没有通过 Beta 推进。有人可以告诉我这里缺少什么吗?
【问题讨论】:
【参考方案1】:OP 的目标是在三个单独的电子表格中复制三张工作表;阿尔法、贝塔和卡帕。 OP 的代码适用于 Kappa,但不适用于 Alpha 或 Beta。
为什么这段代码有效而 OP 代码无效 好吧,OP 代码确实有效,但是 OP 通过指定一个固定范围(30000 行长和大约 40 列宽)来伪造。这适用于第一张纸,但不适用于第二张和第三张纸。以下代码与 OP 代码在两个主要方面不同:
-
使用
getLastRow
和getLastColumn
评估Alpha、Beta、Kappa 和“原始提取”中的数据范围。所以源范围和目标范围将相互匹配。此外,第二张和第三张工作表中的数据很容易在前最后一行下方附加一行。
copyTo
代码更改为复制整个工作表而不是一个范围。原因是前面四个命令setValues(values)
、setBackgrounds(bGcolors)
、setFontColors(colors)
和setFontSizes(fontSizes)
已经将数据范围复制到“Raw Extract”。
function so_55448299()
var version = 'ver. 4.01'
var ssnew = SpreadsheetApp.create('V5 Raw Extract ' + version); //create a spreadsheet
var ssnewsheet = ssnew.getSheetByName('Sheet1'); //get the sheet named Sheet1
ssnewsheet.setName('V5 Raw Extract'); //rename the sheet name of the newly created spreadsheet
// copy Kappa and data
var ssKappa = SpreadsheetApp.getActiveSpreadsheet(); //opens source file
var targetss = ssnew; //define created ss as the target ss
var srcSheetKappa = ssKappa.getSheetByName('KAPPA'); //get source sheet name
var targetSheet = targetss.getSheetByName('V5 Raw Extract'); //defining target sheet
// get the last row and column of the source sheet
var kappaLastRow = srcSheetKappa.getLastRow();
var kappaLastCol = srcSheetKappa.getLastColumn();
//Logger.log("DEBUG: Kappa Last row = "+kappaLastRow+", Last column = "+kappaLastCol);//DEBUG
// declare the source and target ranges
var srcRangeKappa = srcSheetKappa.getRange(1, 1, kappaLastRow, kappaLastCol); //get source data
var destRangeKappa = targetSheet.getRange(1, 1, kappaLastRow, kappaLastCol); //define target
// get values and other data
var values = srcRangeKappa.getValues(); //line 18 to 21 is just to match source sheet and target sheet
var bGcolors = srcRangeKappa.getBackgrounds();
var colors = srcRangeKappa.getFontColors();
var fontSizes = srcRangeKappa.getFontSizes();
// set values and other data
destRangeKappa.setValues(values);
destRangeKappa.setBackgrounds(bGcolors);
destRangeKappa.setFontColors(colors);
destRangeKappa.setFontSizes(fontSizes);
// duplicate the entire sheet
srcSheetKappa.copyTo(targetss);
// end copy Kappa and data
// start copy Alpha and data
var ssAlpha = SpreadsheetApp.openById("<insert code here>");
var srcSheetAlpha = ssAlpha.getSheetByName('V5 ALPHA');
// get the last row and column of the source sheet
var alphaLastRow = srcSheetAlpha.getLastRow();
var alphaLastCol = srcSheetAlpha.getLastColumn();
//Logger.log("DEBUG: Alpha Last row = "+alphaLastRow+", Alpha Last Column = "+alphaLastCol);//DEBUG
// get the last row and column of the target sheet
var rawLastRow = targetSheet.getLastRow();
var rawLastCol = targetSheet.getLastColumn();
Logger.log("DEBUG: Target Last row = " + rawLastRow + ", Target Last Column = " + rawLastCol); //DEBUG
// declare the source and target ranges
var srcRangeAlpha = srcSheetAlpha.getRange(2, 1, alphaLastRow - 1, alphaLastCol);
var destRangeAlpha = targetSheet.getRange(rawLastRow + 1, 1, alphaLastRow - 1, alphaLastCol);
//Logger.log("DEBUG:destRangeAlpha = "+destRangeAlpha.getA1Notation());//DEBUG
// get values and other data
var values = srcRangeAlpha.getValues();
var bGcolors = srcRangeAlpha.getBackgrounds();
var colors = srcRangeAlpha.getFontColors();
var fontSizes = srcRangeAlpha.getFontSizes();
// set values and other data
destRangeAlpha.setValues(values);
destRangeAlpha.setBackgrounds(bGcolors);
destRangeAlpha.setFontColors(colors);
destRangeAlpha.setFontSizes(fontSizes);
// duplicate the entire sheet
srcSheetAlpha.copyTo(targetss);
// end copy Alpha and data
// start copy Beta and data
var ssBeta = SpreadsheetApp.openById("<insert code here>");
var srcSheetBeta = ssBeta.getSheetByName('V5 BETA');
// get the last row and column of the source sheet
var betaLastRow = srcSheetBeta.getLastRow();
var betaLastCol = srcSheetBeta.getLastColumn();
//Logger.log("DEBUG: Beta Last row = "+betaLastRow+", Beta Last Column = "+betaLastCol);//DEBUG
// get the last row and column of the target sheet
var rawLastRow = targetSheet.getLastRow();
var rawLastCol = targetSheet.getLastColumn();
//Logger.log("DEBUG: Target Last row = "+rawLastRow+", Target Last Column = "+rawLastCol);//DEBUG
// declare the source and target ranges
var srcRangeBeta = srcSheetBeta.getRange(2, 1, betaLastRow - 1, betaLastCol);
var destRangeBeta = targetSheet.getRange(rawLastRow + 1, 1, betaLastRow - 1, betaLastCol);
//Logger.log("DEBUG:destRangeBeta = "+destRangeBeta.getA1Notation());//DEBUG
// get values and other data
var values = srcRangeBeta.getValues();
var bGcolors = srcRangeBeta.getBackgrounds();
var colors = srcRangeBeta.getFontColors();
var fontSizes = srcRangeBeta.getFontSizes();
// set values and other data
destRangeBeta.setValues(values);
destRangeBeta.setBackgrounds(bGcolors);
destRangeBeta.setFontColors(colors);
destRangeBeta.setFontSizes(fontSizes);
// duplicate the entire sheet
srcSheetBeta.copyTo(targetss);
// end copy Beta and data
更新 - 一个小功能
@tehhowch 正确地观察到管理此过程的更合适的方法是使用驱动程序函数中的几个参数调用一个小函数。这是我当时的想法,但(无论好坏)我觉得长期的方法将使 OP 能够更好地理解代码与他们自己的代码有何不同。然而,这种变体旨在满足 tehhowch 的观察。为了 OP 的利益,我保留了 Logger 语句(这解释了代码的异常长度
function so_55448299_04()
//Note#1: this function assumes that it is located in the "Kappa" sheet
//Note#2: the spreadsheet ID for Alpha and Beta cannot be assigned to a variable. they must be entered longhand BEFORE this function is processed.
//Note#3: the target sheet names are also entered longhand. BUT the number of sheets is described in the variable "usersheets"
// user defined variables
var targetVn = 'ver. 4.01';
var targetName = 'V5 Raw Extract';
var usersheets = 3;
// create the targetspreadsheet and starting sheet
var targetss = SpreadsheetApp.create(targetName + " " + targetVn);
var targetSheet = targetss.getSheetByName('Sheet1');
targetSheet.setName(targetName);
//loop through the usersheets
for (var i = 0; i < usersheets; i++)
// if i==0, then process this sheet - KAPPA
if (i == 0)
//Logger.log("DEBUG: This is KAPPA");//DEBUG
var srcss = SpreadsheetApp.getActiveSpreadsheet();
//Logger.log("DEBUG: this spreadsheet is "+srcss.getName());//DEBUG
// startrow is 1 in order to include headers
var startrow = 1;
var srcSheet = srcss.getSheetByName("v5 KAPPA");
// if i=1, then process Alpha
else if (i == 1)
//Logger.log("DEBUG: This is ALPHA");//DEBUG
var srcss = SpreadsheetApp.openById("<Insert code>");
//Logger.log("DEBUG: this spreadsheet is "+srcss.getName());//DEBUG
// startrow is 2 in iorder to avoid duplicating headers
var startrow = 2;
var srcSheet = srcss.getSheetByName("v5 ALPHA");
// if i=2, then process Beta
else if (i == 2)
//Logger.log("DEBUG: This is BETA");//DEBUG
var srcss = SpreadsheetApp.openById("<Insert code>");
//Logger.log("DEBUG: this spreadsheet is "+srcss.getName());//DEBUG
// startrow is 2 in iorder to avoid duplicating headers
var startrow = 2;
var srcSheet = srcss.getSheetByName(usersheets[i]);
// run the subroutine to copy and paste data
// Logger.log("DEBUG: srcSheet: "+srcSheet.getName()+", targetSheet: "+targetSheet.getName()+", startrow: "+startrow+", targetss: "+targetss.getName());//DEBUG
var getresult = getData04(srcSheet, targetSheet, startrow, targetss);
function getData04(srcSheet, targetSheet, startrow, targetss)
//get the source sheet - last row and column
var srcLastRow = srcSheet.getLastRow();
var srcLastCol = srcSheet.getLastColumn();
//Logger.log("DEBUG: Source Last row = "+srcLastRow+", Last column = "+srcLastCol);//DEBUG
// get the target sheet - last row and column
var targetLastRow = targetSheet.getLastRow();
var targetLastCol = targetSheet.getLastColumn();
//Logger.log("DEBUG: Target Last row = "+targetLastRow+", Target Last Column = "+targetLastCol);//DEBUG
// declare the source and target ranges
if (startrow == 1)
var srcRange = srcSheet.getRange(startrow, 1, srcLastRow, srcLastCol);
var targetRange = targetSheet.getRange(startrow, 1, srcLastRow, srcLastCol);
else
var srcRange = srcSheet.getRange(startrow, 1, srcLastRow - 1, srcLastCol);
var targetRange = targetSheet.getRange(targetLastRow + 1, 1, srcLastRow - 1, srcLastCol);
//Logger.log("DEBUG: srcRange = "+srcRange.getA1Notation()+", target range = "+targetRange.getA1Notation());//DEBUG
// get source values and other data
var values = srcRange.getValues();
var bGcolors = srcRange.getBackgrounds();
var colors = srcRange.getFontColors();
var fontSizes = srcRange.getFontSizes();
// set values and other data
targetRange.setValues(values);
targetRange.setBackgrounds(bGcolors);
targetRange.setFontColors(colors);
targetRange.setFontSizes(fontSizes);
// duplicate the entire sheet
srcSheet.copyTo(targetss);
var result = "Successful";
return result;
【讨论】:
我觉得这应该是一个用驱动函数的几个参数调用的小函数.. @tehhowch Mea Culpa。我自己也有这种感觉,并进行了修订以反映更有效的方法。该功能并不像人们想象的那么“小”。您可能会看到如何改进它。以上是关于将数据从一张电子表格复制到另一张电子表格的主要内容,如果未能解决你的问题,请参考以下文章
Vlookup 公式将一些数据从一张表提取到另一张表(谷歌电子表格)
Google 表格/AppScript - 将数据从一张表格复制到另一张表格