简体   繁体   English

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

[英]Copy a range from another spreadsheet to another

I stumbled upon a code here a while ago which works to copy a range from one sheet to another within the same spreadsheet.不久前,我在这里偶然发现了一个代码,它可以在同一个电子表格中将一个范围从一个工作表复制到另一个工作表。

var source_sheet = ss.getSheetByName("Sheet1");
var target_sheet = target.getSheetByName("Sheet2");

var url = "#gid=" + newSheet.getSheetId()

var source_range = source_sheet.getActiveRange();
source_range = source_sheet.getRange(source_range.getRow(), 1, 1, 3);
source_sheet.getRange("I" + source_range.getRow()).setFormula('=IFERROR(Sheet2!C4," ")')
source_sheet.getRange("K" + source_range.getRow()).setFormula('=HYPERLINK("' + url + '","Generated Sheet")')
var target_range = target_sheet.getRange("B4");

source_range.copyTo(target_range);

The script works as intended but now I want to copy from one spreadsheet to another.该脚本按预期工作,但现在我想从一个电子表格复制到另一个。 And when I set the a sheet from a different spreadsheet as the target sheet, I get the error of source and target must be within the same spreadsheet.当我将来自不同电子表格的工作表设置为目标工作表时,我得到源和目标必须在同一个电子表格中的错误。

Therefore, is there any workaround for this?因此,有什么解决方法吗? I was think maybe using importrange, but how do I specify the URL and that selected range in the source range to be imported using the insert function script.我想也许使用 importrange,但我如何指定 URL 以及要使用插入 function 脚本导入的源范围中的选定范围。 If you can think of any other solution, please do let me know.如果您能想到任何其他解决方案,请告诉我。

source_range.copyTo(target_range);

You're getting this error message because copyTo() method is based on a range AND assumes that the source and target are in the same spreadsheet.您收到此错误消息是因为copyTo() 方法基于范围并且假定源和目标位于同一个电子表格中。

The more straightforward method to copy a range is to use setValues , even though there are two spreadsheets and two different sheets.复制范围更直接的方法是使用setValues ,即使有两个电子表格和两个不同的工作表。 The following script demonstrates this.以下脚本演示了这一点。


function so5894550501() {

  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var sourcesheetname = "sourcesheet";
  var source = ss.getSheetByName(sourcesheetname);

  var targetid = "<insert targetid>";
  var ts = SpreadsheetApp.openById(targetid);
  var targetsheetname = "targetsheet";
  var target = ts.getSheetByName(targetsheetname);

  var sourceRange = source.getRange(1,1,source.getLastRow(),source.getLastColumn());
  Logger.log("the source range = "+sourceRange.getA1Notation())
  var sourceValues = sourceRange.getValues();
  Logger.log(sourceValues);
  Logger.log(sourceValues.length)


  var targetRange = target.getRange(1,1,source.getLastRow(),source.getLastColumn());
  targetRange.setValues(sourceValues);// this works

}

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

相关问题 将范围从一个电子表格复制到另一个 - Copy a range from one spreadsheet to another 将动态范围从一个电子表格复制到另一个使用getfileid调用的电子表格 - copy dynamic range from one spreadsheet to another spreadsheet called with getfileid 根据过滤范围将数据从电子表格复制到另一个电子表格 - Copy data from a SpreadSheet to another based on filtered range 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? 将值从一个范围复制到另一个电子表格末尾的脚本 - Script to copy values from a range to the end of another spreadsheet 将过滤后的范围从一个电子表格复制到另一个 - Google 应用脚本 - Copy filtered range from one spreadsheet to another - Google app script 应用程序脚本将多个范围从一张工作表复制到另一个电子表格 - Apps script copy multiple range from 1 sheet to another spreadsheet 将范围复制到另一个使用模板创建的电子表格 - Copy range to another spreadsheet created using a template 将电子表格中的多行复制到另一个电子表格 - Copy multiple rows from a spreadsheet to another spreadsheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM