本文介绍了我写了一个脚本将文件复制到备份文件夹,但是将独立的Webapp文件复制到了根目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人知道为什么会这样吗?将所有文件复制到目标位置将更加方便.我有一个文件名和文件ID的列表3是电子表格,一个是我作为Web应用程序部署的独立脚本.这三个电子表格会转到正确的位置.独立的webapp进入根目录.我不认为这是重复的建议.
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');
}
推荐答案
我通过使用Drive API的更新"方法修改了脚本.复制文件后,此脚本的流程如下.
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.
- 使用
makeCopy()
复制文件. - 如果文件的mimeType是Google Apps脚本,则使用Drive API的更新"方法将文件移到
subFldr
. - 如果文件的mimeType不是Google Apps脚本,则不使用更新"方法.
修改后的脚本:
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');
}
注意:
- 在此修改后的脚本中,使用了高级Google服务的Drive API.因此,请在高级Google服务和API控制台中启用Drive API.如果是高级Google服务,则Drive API的版本为v2.
- 您可以在.
这篇关于我写了一个脚本将文件复制到备份文件夹,但是将独立的Webapp文件复制到了根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!