简体   繁体   English

将工作簿 A 的工作表 1 复制到工作簿 B 的工作表 1

[英]Copy Sheet 1 from Workbook A to Sheet 1 of Workbook B

I'm trying to copy sheets from what I'm calling a "Master" workbook to a workbook that my staff has access to but I can't seem to get it to copy.我正在尝试将我称之为“主”工作簿的工作表复制到我的员工可以访问的工作簿,但我似乎无法复制它。

Once I have the script, I'm going to set it up to copy every day.一旦我有了脚本,我就会将它设置为每天复制。 That way I can edit the Master and it will essentially "auto update" for my staff every day.这样我就可以编辑 Master,它基本上每天都会为我的员工“自动更新”。

function copyDoc() {
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('General');

var targetSheet = SpreadsheetApp.openById('13ERWTQoXeGb0cIfgfr2ZTsImnwmtaVB5c-Zm9ZZ5o6g').getSheetByName('General');

sourceSheet.copyTo(targetSheet);
}

It just tells me there's an error:它只是告诉我有一个错误:

Error错误
Exception: The parameters (number,number,number,null) don't match the method signature for SpreadsheetApp.Sheet.getRange.异常:参数 (number,number,number,null) 与 SpreadsheetApp.Sheet.getRange 的方法签名不匹配。 copyDoc @ Test Copy.gs:11 copyDoc @ 测试 Copy.gs:11

I have absolutely no idea what I'm doing or what I should be doing.我完全不知道我在做什么或我应该做什么。 Essentially I need to copy Sheet 1 through 7 from Workbook A into Sheet 1 through 7 of Workbook B every single day.本质上,我需要每天将工作簿 A 中的工作表 1 到工作表 7 复制到工作簿 B 的工作表 1 到工作表 7 中。 But the sheets are all already in each Workbook and titled.但是这些工作表都已经在每个工作簿中并带有标题。

截屏

The screenshot shows that your project has 4 files.屏幕截图显示您的项目有 4 个文件。

The file having the function that you are trying to execute is in file Copy Daily.gs .您尝试执行的具有 function 的文件位于文件Copy Daily.gs中。 This file has only 7 lines.这个文件只有 7 行。 The error doesn't belong to that file (as explained below).该错误不属于该文件(如下所述)。

  1. Test Copy.gs is the file name having the statement causing the error. Test Copy.gs是包含导致错误的语句的文件名。
  2. 11 is the line number of the Test Copy.gs file. 11Test Copy.gs文件的行号。
  3. getRange is the method having the problem getRange是有问题的方法

It's very likely that the error is occurring because the referred line from file Test Copy.gs is in the global scope. Statements in the global scope, no matter on which file they are, are executed before executing the function called.发生错误的可能性很大,因为文件Test Copy.gs中引用的行位于全局 scope 中。全局 scope 中的语句,无论它们在哪个文件中,都在执行调用的 function 之前执行。

Possible fix:可能的修复:

  1. Open the Test Copy.gs file打开Test Copy.gs文件
  2. Add // at the beginning of line 11在第 11 行的开头添加//

Another option is to delete the Test Copy.gs file.另一种选择是删除Test Copy.gs文件。 If you have valuable information on that file, before deleting it, make an appropriate backup.如果您在该文件上有有价值的信息,请在删除它之前进行适当的备份。

Related有关的

I asked on Reddit and this ended up working!我在 Reddit 上询问,结果成功了!

function copyDoc() {
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('General');
var target = SpreadsheetApp.openById('13ERWTQoXeGb0cIfgfr2ZTsImnwmtaVB5c-Zm9ZZ5o6g');
sourceSheet.copyTo(target);
target.deleteSheet(target.getSheetByName("General"));
var targetSheet = target.getSheetByName("Copy of General")
target.setActiveSheet(targetSheet)
target.moveActiveSheet(1);
targetSheet.setName("General");
}

I then just copied and pasted it and changed the name of the sheets for it work across all of the sheets I have in one document.然后我只是复制并粘贴它并更改了工作表的名称,因为它适用于我在一个文档中的所有工作表。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM