Reputation: 289
I have the following script that when end of run, I want to make a copy of that spreadsheet into a specific folder with name + date, for example:
I have the spreadsheet called "Audit", When some specific script run at the end I want to make a copy for the spreadsheet into the folder root/Audit/Historic into google driver or google docs.
Note: root is the google driver or google docs top folder.
Script:
function ShellCopyValues(){
var sheets = ['sheet1','sheet2','sheet3','sheet4','sheet4'];
for (var s in sheets){
CopyRows(sheets[s]);
}
}
function CopyRows(uname) {
var source = SpreadsheetApp.openById('XXXXXX');
var sourcesheet = source.getSheetByName(uname);
var target = SpreadsheetApp.openById('YYYYYY');
var targetsheet = target.getSheetByName(uname);
var targetrange = targetsheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn());
var rangeValues = sourcesheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn()).getValues();
targetrange.setValues(rangeValues);
}
At the end of this script I want to make a copy.
I will appreciate if anyone have any idea about how can I do it.
The script will save the file into a folder. (Will not appear in the root "My Driver")
//This part make a copy of the spreadsheet
var SSID = 'XXXXXX'
var CopyDate = Utilities.formatDate(new Date(), "GMT-3", "ddMMyyyyHHmm"); // Function Date + Format
var folder = DocsList.getFolder('Historic'); //Use this line if you want to get the folder by name
//--> var folder = DocsList.getFileById('YYYYY'); //Use this line if you want to get the folder by ID
var backup = DocsList.getFileById(SSID).makeCopy(SpreadsheetApp.openById(SSID).getName() + "_" + CopyDate);
backup.addToFolder(folder); //This line will be move the folder
backup.removeFromFolder(DocsList.getRootFolder()); //This line is needed to remove the Filde from the Root
Upvotes: 1
Views: 4202
Reputation: 16551
Particularly used addToFolder(parent), removeFromFolder(parent) and getParents().
Try:
...
var folder = DocsList.getFolder('Historic');
var backup = DocsList.getFileById(SSID).makeCopy(SpreadsheetApp.openById(SSID).getName() + "_" + CopyDate);
var deleteFromFolder = backup.getParents()[0];
backup.addToFolder(folder);
backup.removeFromFolder(deleteFromFolder);
...
Upvotes: 0
Reputation: 1345
Use DriveApp
... get the file using getFileById(id)
then use makeCopy(name)
then
Use DocsList
... get the file using getFileById(id)
then use addToFolder(folder)
I don't think there is an addtofolder
in DriveApp
and the file objects in DocsList
and DriveApp
are not the same
Upvotes: 1