LAD Service Desk
LAD Service Desk

Reputation: 289

How can I do a copy of my spreadsheet into specifc folder in google drive using Google Script

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.

EDIT: 08/07/2013 (Fixed)

Upvotes: 1

Views: 4202

Answers (2)

wchiquito
wchiquito

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

DavidF
DavidF

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

Related Questions