![](/img/trans.png)
[英]can I reference the results of an ArrayFormula recursively in Google Sheets?
[英]Avoiding exceeding execution time when recursively converting Google Sheets to Excel
我正在編寫一個腳本,以檢查Google雲端硬盤中的一系列文件夾,制作文件夾結構的副本,並將文件夾中包含的文件轉換為Excel或Word。
共有三個選項:Google表格文件應轉換為Excel; 將Google Docs文件轉換為Word; 其他文件應照原樣復制。
我正在使用遞歸函數來探索文件結構。
該腳本似乎可以運行,但是我超出了允許的執行時間。 分析執行記錄,這似乎是由於將每個Google文件轉換為MS-Office文件大約需要2秒鍾的時間。
誰能建議:
抱歉,如果已解決此問題,我確實花了一些時間來尋找答案,但我可以看到類似的問題,但我無法理解的解決方案。
下面的代碼。
function folderRecurse(folderToSearch, folderToSave){
while (folderToSearch){ // the function has been passed a folder to search inside
var searchFolderId = folderToSearch.getId(); // get the Id of the folder passed
var currentFolder = DriveApp.getFolderById(searchFolderId); //open the search folder passed
var newName = currentFolder.getName(); // get the name of the search folder passed
var saveFolder = folderToSave.createFolder(newName);// make a copy of the search folder in the backup folder
var subfolders = currentFolder.getFolders(); //get any subfolders of the folder passed
while (subfolders.hasNext()){ // if there are subfolders, start again by passing the function the folder to search and the folder to save to
var subfolder = subfolders.next();
folderRecurse(subfolder, saveFolder);
}
// when there are no more folders left, deal with the files inside the folder
var folderFiles = folderToSearch.getFiles();
while (folderFiles.hasNext()){
// get the file
var thisFile = folderFiles.next();
// get the file's name
var thisFileName = thisFile.getName();
//test the file type
var fileType = thisFile.getMimeType();
Logger.log(fileType);
if (fileType == "application/vnd.google-apps.document"){ // this is a google doc-- save it as a word document
//google docs to word method
}
else if (fileType == "application/vnd.google-apps.spreadsheet"){ // this is a spreadsheet -- save it as an excel file
convertToXlsxAndSave(thisFile,saveFolder)
Logger.log(thisFile.getName());
}
else { // save it as whatever kind of file it is
thisFile.makeCopy(thisFile.getName(),saveFolder);
}
}
return 0;
}
return 0;
}
function convertToXlsxAndSave(thisFile, saveFolder){
//open the file as an excel sheet
var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + thisFile.getId() + "&exportFormat=xlsx";
// set parameters
var params = {
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
};
// get the binary
var blob = UrlFetchApp.fetch(url, params).getBlob();
// name the binary with the fileName
blob.setName(thisFile.getName() + ".xlsx");
// save the binary into the proper folder
saveFolder.createFile(blob);
}
function convertToDocxAndSave(thisFile, saveFolder){
//open the file as a word doc sheet
var url = "https://docs.google.com/feeds/download/documents/export/Export?id=" + thisFile.getId() + "&exportFormat=docx";
// set parameters
var params = {
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
};
// get the binary
var blob = UrlFetchApp.fetch(url, params).getBlob();
// name the binary with the fileName
blob.setName(thisFile.getName() + ".docx");
// save the binary into the proper folder
saveFolder.createFile(blob);
}
function createMsOfficeVersions() {
// this function works through the files in the folders enumerated below, and creates MS Office versions. Eg Google Sheets convert to Excel files, Google Docs to Word files. The files are stored in a folder for downloading to AC.
// create a date-stamped folder within the backups folder
var formattedDate = Utilities.formatDate(new Date(), "GMT+12", "dd MMMM yyyy");
var backupLabel = "HAMP Survey MS Office backup versions " + formattedDate;
// get the folder Ms Office versions
var backupDir = DriveApp.getFolderById('XXXXXXX');
// make a new date-stamped folder to save the files into
var thisBackup = backupDir.createFolder(backupLabel);
// get the Survey 2015-2016 folder and subfolders
var hampSurveyFolder = DriveApp.getFolderById('XXXXXXXXX');
// loop through all the folders. For each, create a new folder inside the backup folder, grab all the files, test if they are docs or spreadsheets, copy to the new folder. if they are neither, copy as is.
folderRecurse(hampSurveyFolder,thisBackup);
}
如果Google雲端硬盤文件夾中包含大量文件,則腳本可能會超時。 相反,您應該使用基於時間的觸發器,該觸發器每10分鍾運行一次並轉換文件,同時文件迭代器應作為屬性存儲,以從上次中斷的地方繼續。
function myFunction() {
var props = PropertiesService.getScriptProperties();
var token = props.getProperty('TOKEN');
var files = continuationToken ? DriveApp.continueFileIterator(token) : DriveApp.getFiles();
var start = Date.now();
while (files.hasNext() && (Date.now() - start < 5*60*1000)) {
var file = files.next();
// convert files
}
if (files.hasNext()) {
props.setProperty("TOKEN", files.getContinuationToken());
} else {
props.deleteProperty("TOKEN");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.