Duplicate a tab across multiple workbooks in google - duplicates

I'm hoping someone can help me with this?
I have multiple spreadsheets in a file all with a tab named after the week start date.
What i want to do is duplicate this tab in all the spreadsheets with one google script.
At present I have a macro linked button in each spreadsheet with the following code -
function NewWeek() {
const sh = SpreadsheetApp.getActive();
let ss = sh.duplicateActiveSheet();
let name = ss.getRange("E4").getDisplayValue();
ss.setName(name);
}
Is there a way for me to have this run across all spreadsheets without having to open them individually and run the macro?
Any help would be most appreciated.

Related

Google sheets make a copy of a worksheet to drive folder

I Am wondering if anyone can help.
I have been making a collection log sheet in google sheets and i want to only make a copy of 1 of the work sheets at the end of each month.
for exaple i want this worksheet tab in google sheets called collected orders to be copyed and then the sheet to be cleared of data arfter it had been copyed to the google drive
https://i.imgur.com/m5GueWv.png
The Apps Script reference documentation is your friend https://developers.google.com/apps-script/reference/spreadsheet.
The following code may or may not answer your question as it isn't completely clear what your actual workflow is.
What the code below will do is;
Get the active spreadsheet
Get the sheet called Completed Orders
Copy that sheet to the active spreadsheet
Get the range of data on that newly copied sheet
Delete the cells specified by that range of data
function copySheetDeleteData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mySheet = ss.getSheetByName("Completed Orders");
var myNewSheet = mySheet.copyTo(ss);
var copiedData = myNewSheet.getDataRange()
copiedData.deleteCells(SpreadsheetApp.Dimension.ROWS)
}
When I read your question I think;
Does he want to schedule this script to run at the end of the month?
Does the spreadsheet need to be open at the time?
When he says worksheet, does he mean the spreadsheet or the sheet within a spreadsheet?
Why bother copying the sheet and the data and then delete the data?
Why not just create a new sheet and call it what you want?

Wishing to run calendar import script on specific sheet not on active sheet

I would like the Google Calendar import script run on a specific sheet. Lets Say "Sheet4" The script works well on any current active sheet but I seem to be unable to modify all the proper lines in the script to make it work properly to be directed to a specified sheet.
Here is a link to the sheet
https://docs.google.com/spreadsheets/d/1DI_gnXyeKrR_t_6u0-EKzfUjeMmXj8GC6HlU-3uGmkc/edit#gid=2105065853
I tried to paste the code but kept getting formatting error. Apologies in advance. I am very new to this
You haven't shared the Google Spreadsheet but you can use the below code snippet to perform task on a particular sheet.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet4");
For sheet of different spreadsheet.
var sheet = SpreadsheetApp.openById("<<ENTER SPREADSHEET ID>>").getSheetByName("Sheet4")

Google Sheets script, copyto and replace existing sheet

I've been searching for the past few weeks and haven't been able to figure out this script. What I'm trying to do is figure out a way for me to duplicate a pictorial calendar on a master Google Sheets file to another Google Sheets file that I manage. Every time there's an update on the master calendar, I'd love for it to automatically update in the other Google Sheets file that I manage. Right now I have to copy paste everything, reformat, and reimport images. I did find a script that somewhat works, but it has some issues that cause it to be just as much work.
The script is a copyTo script, it allows me to copy a sheet titled "Pictorial Calendar" from the master Google Sheets file (called "Master Calendar") to a different Google Sheets file (called "My Calendar"). I even set up a trigger that allows the script to run whenever I make an edit.
However, it keeps creating new sheets titled "Copy of Pictorial Calendar 1" "Copy of Pictorial Calendar 2" etc.) and I can't figure out how to make the script replace the existing sheet instead of creating new sheets.
I also would love to be able to change the duplicated sheet to just be called "Pictorial Calendar" in the "My Calendar" file. The script I'm using is:
function copyMasterCalendar() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheet = source.getSheets()[2];
var destination = SpreadsheetApp.openById("1exiUWVypFpYeHMkXHO3sMUTGupiC2gQjZIF0Ss44-pU");
sheet.copyTo(destination);
}
Any and all help would be much appreciated! Thank you!
If I understood you correctly try this...
function copyMasterCalendar() {
var source = SpreadsheetApp.getActiveSpreadsheet(),
sheet = source.getSheetByName('Pictorial Calendar'),
destination = SpreadsheetApp.openById("1exiUWVypFpYeHMkXHO3sMUTGupiC2gQjZIF0Ss44-pU"),
destSheet = destination.getSheetByName('Pictorial Calendar'),
copydSheet = sheet.copyTo(destination);
copydSheet.getDataRange().copyTo(destSheet.getDataRange());
destination.deleteSheet(copydSheet);
}

Unable to access (an save) my SpreadSheet Google App Script

I have a Google SpreadSheet (filled through a form) associated to a Google Apps Script.
Now, the spreadsheet contains too many cells and I'm not able to load it anymore (server timeout).
I'm not interested in the data, but I really want to retrieve the script (the source code).
Unfortunately, the only way I know is to load the SpreadSheet first :-(
(Copying the Document ends in a timeout and the script is not copied.)
Any suggestion?
Regards,
RĂ©mi.
Create another script which makes a copy of original spreadsheet to backup the data and then clear the original spreadsheet through the script.
However this seems an issue which should be reported to Google Spreadsheet Forum
Here is an example code which will backup the spreadsheet as a copy and clear the original spreadsheet
function backupDataAndClear(){
var sourceSS = SpreadsheetApp.openById('ID_OF_ORIGINAL_SS');
var newSS = sourceSS.copy('Backup SS');
var sourceSheets = sourceSS.getSheets();
for(var i in sourceSheets){
sourceSheets[i].clear();
}
}

How to access data on different Google Spreadsheet through Google Apps Script?

I'm writing a Google Apps Script on my Google Site and am trying to use data that is provides on 2 different tabs in a Google Spreadsheet. From what I thought I understood from the documentation I could use all available methods in the SpreadsheetApp class on a Sites script by just using the openById() method.
Anyway here is what I tried to do
function doGet(e) {
var doc = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE).getActiveSheet();
SpreadsheetApp.setActiveSheet(doc.getSheetId()[1]);
//....
}
I get the error
Cannot find method setActiveSheet(. (line 4)
I'm pulling my work off this link: Storing Data in Google Spreadsheets and also the Ui Service section listed under Building User Interfaces.
Anybody seeing what I'm doing wrong in these two lines?
setActiveSheet should be used only with the spreadsheet displayed by the UI, a sheet in a spreadsheet you have opened in your browser.
With SpreadsheetApp.openById you are opening a spreadsheet to access its data, but it doesn't open in your browser. It hasn't an UI.
I found this comments in https://developers.google.com/apps-script/class_spreadsheetapp?hl=es-ES#openById :
// The code below opens a spreadsheet using it's ID and gets the name for it.
// Note that the spreadsheet is NOT physically opened on the client side.
// It is opened on the server only (for modification by the script).
var ss = SpreadsheetApp.openById("abc1234567");
Some examples assume your script is running into your spreadsheet. It's not your case, because you are running a script as a service, which should have its own User Interface.
I think #megabyte1024 addresses the syntax errors, but in answer to your comment to #YoArgentina:
Do you happen to know of a way to access data on different tabs then
through a service not running inside the Spreadsheet?
Does this sort of help?
var ss = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE);
var sheets = ss.getSheets();
// the variable sheets is an array of Sheet objects
var sheet1A1 = sheets[0].getRange('A1').getValue();
var sheet2A1 = sheets[1].getRange('A1').getValue();
You need to access each sheet separately.
var ss = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE);
var sheet = ss.getSheets()[0]; // "access data on different tabs"
ss.setActiveSheet(sheet);
There is at least one problem in these two lines. The 1st one is that the setActiveSheet method parameters is a Sheet class object and the getSheetId method returns an integer value. By the way this method (getSheetId) is not documented. The 2nd problem can happen if the SpreadsheetApp has no active spreadsheet. In this case there is the "Please select an active spreadsheet first." error. Use the SpreadsheetApp.setActiveSpreadsheet method to set an active spreadsheet.