简体   繁体   English

将范围从一个电子表格复制到另一个

[英]Copy a range from one spreadsheet to another

I am attempting to copy the contents of an array from one sheet (where the array is created by iterating through and pushing select items for the column of the user's choosing) to a different sheet in a different spreadsheet.我试图将数组的内容从一张工作表(通过迭代并推送用户选择的列的选择项来创建数组)复制到不同电子表格中的另一张工作表。

I have come across a number of questions and answers on how to import a range from one spreadsheet to another, but none of them has worked for me (all returning the error "We're sorry, a server error occurred. Please wait a bit and try again.") I've tried 'copyTo' as well, with no success.我遇到了许多关于如何将范围从一个电子表格导入到另一个电子表格的问题和答案,但没有一个对我有用(都返回错误“我们很抱歉,发生服务器错误。请稍等一下然后再试一次。”)我也试过“copyTo”,但没有成功。

Here is the code I'm using currently.这是我目前使用的代码。 Where have I gone wrong??我哪里错了??

function copyToTrix(featureList) {

  featureList = featureList.getValues();

  var tss = SpreadsheetApp.openById("0AtX9IYFZ..."); //define active trix as target 

var ts = tss.getSheetByName("US");          //define sheet in target trix

var newRange = ts.getRange("DA12:DA25");    //define target range

 newRange.setValues(featureList);  //set values in target range to items in featureList

Browser.msgBox('copied');
}

You defined your target range in a way that is quite dangerous as it relies only on your decision and not on the array content... You could use this method that works all the time :您以一种非常危险的方式定义了目标范围,因为它仅依赖于您的决定而不是数组内容……您可以使用这种一直有效的方法:

var beginning_row = 12;
var beginning_col = /*what ever corresponds to 'DA' */ ;
var newRange = ts.getRange(beginning_row, beginning_col, featureList.length, featureList[0].length);    //define target range

it takes the number of rows from the array length (which is actually what it is) and the number of columns from the length of its first element (same comment)... quite straightforward and reliable ;-)它从数组长度中获取行数(实际上就是它)和从其第一个元素的长度中获取列数(相同的注释)......非常简单和可靠;-)

Also : the answer you gave to yourself is wrong (sorry about that, no personal offense...) as it leads to the exact contrary of what is described in the 'best practice' chapter of the doc and I'd recommend to remove its 'answered mark' and upvote.另外:你给自己的答案是错误的(抱歉,没有个人冒犯......)因为它导致与文档“最佳实践”一章中描述的完全相反,我建议删除它的“回答标记”和upvote。

I am using somepart of someone here, I this code is working for me.我在这里使用某人的某些部分,我这段代码对我有用。

function CopyRange() {
 var sss = SpreadsheetApp.openById('spreadsheetid'); //replace with source ID
 var ss = sss.getSheetByName('sheetname'); //replace with source Sheet tab name
 var range = ss.getRange('A2:G50'); //assign the range you want to copy
 var data = range.getValues();

 var tss = SpreadsheetApp.openById('SpreadsheetID'); //replace with destination ID
 var ts = tss.getSheetByName('sheetname2'); //replace with destination Sheet tab name

 ts.getRange(ts.getLastRow()+1, 1,49,7).setValues(data); //you will need to define the size of the copied data see getRange()

}


This code, is getting the range A2:G50 and copied to sheetname2 in the lastrow available.此代码正在获取范围 A2:G50 并复制到最后一行中的 sheetname2 可用。 For example if sheet2 have the first row availble un row11, this code will paste the information in Row11.例如,如果 sheet2 有第一行可用 un row11,则此代码会将信息粘贴到 Row11 中。

I use this function to copy an entire sheet's (values only) between documents:我使用此功能在文档之间复制整个工作表(仅限值):

 function updateSourceToTarget(sourceID,sourceName,targetID,targetname){
  Logger.log(sourceID + ' ' + sourceName + ' ' +targetname);
var source = SpreadsheetApp.openById(sourceID).getSheetByName(sourceName);
var destination = SpreadsheetApp.openById(targetID).getSheetByName(targetname);
var sourcelastRow = source.getLastRow();
var sourcelastCol = source.getLastColumn();
var sourcedata = source.getRange(1,1,sourcelastRow,sourcelastCol).getValues();
destination.getRange(1,1,sourcelastRow,sourcelastCol).setValues(sourcedata);
}

then I call this function, in example:然后我调用这个函数,例如:

updateSourceToTarget('sourceID','sourceSheetName','targetID','targetSheetName'); 

getting a replica of the data in the original sheet in a different document.在不同文档中获取原始工作表中数据的副本。 very useful for small documents that You have to keep the original untouched and unshared, if formatting and functions is not important.如果格式和功能不重要,您必须保持原件不变和不共享的小文档非常有用。

another direction is the function sheet.copyto() that creates a new copy of the sheet, but then You require some various garbage disposal functions.另一个方向是创建工作表新副本的函数sheet.copyto() ,但是您需要一些各种垃圾处理功能。 Good luck!祝你好运!

Both copyTo, and setValues work - I use them in my code. copyTo 和 setValues 都可以工作 - 我在我的代码中使用它们。

I would locate the line that produces the error - logs, or a try and catch.我会找到产生错误的行 - 日志,或尝试和捕获。

or add something like this - to see if it gives you some clues或添加这样的东西 - 看看它是否给你一些线索

// The code below will set the values for range A1:D2 to the values in an array.
var myTable = [[1, 2, 3, 4],[5, 6, 7, 8]];
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1,1,2,4).setValues(myTable);

Very quick and short :非常快速和简短:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceData = ss.getRange('Sheet1' + '!A2:M2').getValues();
var targetSheetNextRow = ss.getSheetByName('Sheet2').getLastRow() + 1;
ss.getRange('Sheet2' + '!A' + targetSheetNextRow+':M' + targetSheetNextRow).setValues(sourceData);

It turns out the GAS is having trouble with the setValues() function.事实证明,GAS 的 setValues() 函数有问题。 It wasn't working to take a full range, so I ended up having to use setValue() instead.取一个完整的范围是行不通的,所以我最终不得不使用 setValue() 来代替。 (Set value only allows you to operate in one cell at a time): (设置值只允许您一次在一个单元格中操作):

var tss = SpreadsheetApp.openById("0AtX9IYFZ9KkbdHg4TUdqYVppTTU5OENpb04tWTdNbHc");
var ts = tss.getSheetByName("US");
for (var i = 0; i < featureList.length; i++) {
var position = i + 12;
ts.setActiveCell("B" + position).setValue(featureList[i]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将动态范围从一个电子表格复制到另一个使用getfileid调用的电子表格 - copy dynamic range from one spreadsheet to another spreadsheet called with getfileid Google脚本会根据日期将范围从一个电子表格复制到另一个电子表格 - Google scripts copy range from one spreadsheet to another based on date 是否可以将数据范围从一个电子表格复制到另一个电子表格? - Is possible to copy a data range from one spreadsheet to another? 将过滤后的范围从一个电子表格复制到另一个 - Google 应用脚本 - Copy filtered range from one spreadsheet to another - Google app script 将范围从另一个电子表格复制到另一个 - Copy a range from another spreadsheet to another 将数据从一个电子表格文件复制到另一个 - Copy data from one spreadsheet file to another 在编辑时将数据从一个电子表格复制到另一个电子表格 - Copy data from one spreadsheet to another on edit 将多个工作表从一个电子表格复制到另一个电子表格 - Copy several sheets from one spreadsheet to another spreadsheet 如何将脚本从一个电子表格复制到另一电子表格? - How do I copy a script from one spreadsheet to another spreadsheet? 根据过滤范围将数据从电子表格复制到另一个电子表格 - Copy data from a SpreadSheet to another based on filtered range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM