Home > front end >  Copy Sheet 1 from Workbook A to Sheet 1 of Workbook B
Copy Sheet 1 from Workbook A to Sheet 1 of Workbook B

Time:03-30

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.

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. copyDoc @ Test 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. But the sheets are all already in each Workbook and titled.

Screenshot

CodePudding user response:

The screenshot shows that your project has 4 files.

The file having the function that you are trying to execute is in file Copy Daily.gs. This file has only 7 lines. 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.
  2. 11 is the line number of the Test Copy.gs file.
  3. getRange is the method having the problem

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.

Possible fix:

  1. Open the Test Copy.gs file
  2. Add // at the beginning of line 11

Another option is to delete the Test Copy.gs file. If you have valuable information on that file, before deleting it, make an appropriate backup.

Related

CodePudding user response:

I asked on Reddit and this ended up working!

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.

  • Related