Google sheets make a copy of a worksheet to drive folder - google-apps-script

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?

Related

How to set google spreadsheet circular dependency with Apps Script

Okay I have wrote a function in apps script which is basically copy my sheet and then create a new spreadsheet and paste copied sheet into it.
so my problem is that copied sheet has "if formula" in particular column which worked with circular dependency for adding a date to cell based on checkbox in other column
but when script copied that sheet to other spreadsheet this dependency removed by default.
so, is there any way to set circular dependency with apps script so I can add that line into my code.
Finally Spending little more time on "developers.google.com/apps-script/overview", I have figured out how to tackle this problem there is other class method called..
.setIterativeCalculationEnabled(true)
.setMaxIterativeCalculationCycles(1)
var ss = SpreadsheetApp.getActiveSpreadsheet()
var ws = ss.getSheetByName("SheetName") //sheet name which u want to copy.
var ss2 = SpreadsheetApp.openByID('ID') //Targeted Spreadsheet
ws.copyTo(ss2).setName("AnyName") //give a name
ss2.setIterativeCalculationEnabled(true) //set cicular dependency enable
ss2.setMaxIterativeCalculationCycles(1) // set iteration (1-1000) as per your choice
}
}

How to apply a macro to all sheets in one spreadsheet in Google Sheets

I have a recorded macro in google sheets for one sheet in my spreadsheet. I would like to edit the script so that it will apply to all sheets in the spreadsheet (so that I won't have to run the recorded macro in each sheet separately). I've seen articles on how to do this in Excel but is it possible in Google Sheets?
are you sure it doesn't work on the other sheets? recorded macros usually use the active sheet not a named sheet.
Although I am 9 months late in answering this, here is a working answer to your question with an example to clear all visible sheets using another function called ClearActiveSheet. If someone is still looking for how to do this, this should give an idea.
function ClearActiveSheet() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
sheet.getRange('A1').activate();
sheet.clear();
};
function ClearAllVisibleSheets() {
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
sheets.forEach(function(sheet) {
//You would need to make this the current active sheet, so that you can call the ClearActiveSheet function that works on active sheet
sheet.activate()
// Now since you have this sheet activated, you can call the function to clear the active sheet
ClearActiveSheet();
});
};
Select the function ClearAllVisibleSheets in GoogleSpredsheets from Tools-> ScriptsEditor -> SelectFunction (ClearAllVisibleSheets) and run/or debug and you will see this do the job.
P.S: If this satisfies your need, remember to upvote and kindly accept this as an answer :).

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();
}
}