问题
Does anyone know why this happens? It would be much more convenient to have all the files copied to the destination. I have a list of filenames and file ids 3 are spreadsheets and one is a standalone script which I deploy as a webapp. The three spreadsheet go to the correct place. The standalone webapp goes to the root. I don't think it's a duplicate as suggested.
function backUpProjectFiles(){
var backupFolder=DriveApp.getFolderById(getGlobal('BackupDirId'));
var subFldrName='BackUpFiles_' + Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd_HH:mm:ss');
var subFldr=backupFolder.createFolder(subFldrName);
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('BackUpFiles');
var rg=sh.getDataRange();
var vA=rg.getValues();//column1 is filenames column2 is file ids
var s='<br />Files Backed Up:<br />';
for(var i=1;i<vA.length;i++){
var file=DriveApp.getFileById(vA[i][1]);
file.makeCopy(vA[i][0],subFldr);
s+=Utilities.formatString('%s FilePath:%s/%s/<strong>%s</strong><br />',i,backupFolder.getName(),subFldr.getName(),file.getName());
}
s+=Utilities.formatString('Total Files Copied: %s<br /><input type="button" value="Exit" onClick="google.script.host.close();" />',vA.length-1)
var ui=HtmlService.createHtmlOutput(s).setWidth(600).setHeight(500);
SpreadsheetApp.getUi().showModelessDialog(ui,'File Backup Complete');
}
回答1:
I modified your script by using the "update" method of Drive API. When a file is copied, the flow of this script is as follows.
- Copy a file using
makeCopy()
. - If the mimeType of file is Google Apps Script, the file is moved to
subFldr
using the "update" method of Drive API. - If the mimeType of file is not Google Apps Script, he "update" method is not used.
Modified script :
function backUpProjectFiles(){
var backupFolder=DriveApp.getFolderById(getGlobal('BackupDirId'));
var subFldrName='BackUpFiles_' + Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd_HH:mm:ss');
var subFldr=backupFolder.createFolder(subFldrName);
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('BackUpFiles');
var rg=sh.getDataRange();
var vA=rg.getValues();//column1 is filenames column2 is file ids
var s='<br />Files Backed Up:<br />';
for(var i=1;i<vA.length;i++){
var file=DriveApp.getFileById(vA[i][1]);
var res = file.makeCopy(vA[i][0],subFldr); // Modified
if (file.getMimeType() == MimeType.GOOGLE_APPS_SCRIPT) { // Added
Drive.Files.update({"parents": [{"id": subFldr.getId()}]}, res.getId()); // Added
}
s+=Utilities.formatString('%s FilePath:%s/%s/<strong>%s</strong><br />',i,backupFolder.getName(),subFldr.getName(),file.getName());
}
s+=Utilities.formatString('Total Files Copied: %s<br /><input type="button" value="Exit" onClick="google.script.host.close();" />',vA.length-1)
var ui=HtmlService.createHtmlOutput(s).setWidth(600).setHeight(500);
SpreadsheetApp.getUi().showModelessDialog(ui,'File Backup Complete');
}
Note :
- In this modified script, Drive API of Advanced Google Services is used. So please enable Drive API at Advanced Google Services and API console. In the case of Advanced Google Services, the version of Drive API is v2.
- You can see how to enable Drive API at here.
Reference :
- Advanced Google Services
- update of Drive API v2
来源:https://stackoverflow.com/questions/51959824/i-wrote-a-script-to-copy-files-to-a-backup-folder-but-standalone-webapp-file-get