2014-06-30 101 views
0

我可以用一隻手。我的公司廣泛使用Google表格,當我們失去互聯網連接時,我們需要一種訪問文件的方式。Google Apps腳本:將電子表格保存爲本地備份的ODS

我無法獲得本網站上發現的任何示例,以便通過腳本從Google表格創建xls或ods。

我做了一個腳本來創建csv備份,可以從本地Google Drive文件夾訪問。與小時觸發器一起使用時,此腳本會創建在過去一小時內修改過的每張電子表格的每個工作表的csv文件,並將其放入一個文件夾中,並將其壓縮到特定於備份的文件夾中。從那裏我可以將zip文件移動到我們的本地服務器。

function backUpMaker() { 
    var backupFolderId = '<Id of Backup Folder>'; 
    var timeNow = new Date(); 
    var newFolder = DocsList.createFolder(timeNow); 
    var newFolderId =newFolder.getId(); 
    newFolder.addToFolder(DocsList.getFolderById(backupFolderId)); 
    newFolder.removeFromFolder(DocsList.getRootFolder()); 
    var sheets = DriveApp.getFilesByType(MimeType.GOOGLE_SHEETS); 
    while (sheets.hasNext()) { 
    var sheet = sheets.next(); 
    var lastUpdate = sheet.getLastUpdated(); 
    var timeHourAgo = new Date(); 
    timeHourAgo = timeNow - 3600000; 
    if(lastUpdate >= timeHourAgo){ 
     var sheetId = sheet.getId(); 
     var csv = eachSheet(sheetId,newFolderId); 
    } 
    } 
    var backupFolder = DocsList.getFolderById(backupFolderId); 
    try{ 
    backupFolder.createFile(Utilities.zip(newFolder.getFiles(), timeNow + '.zip')); 
    } catch(err) { 
    Logger.log(err); 
    } 
    newFolder.setTrashed(true); 
} 

function eachSheet(key,newFolderId) { 
    var ss = SpreadsheetApp.openById(key); 
    var ssId = ss.getId(); 
    var ssName = ss.getName(); 
    var howManySheets = ss.getNumSheets(); 
    try{ 
    for (var sheetIndex=0; sheetIndex < howManySheets; sheetIndex++) { 
    var activeSheet = ss.getSheets()[sheetIndex]; 
    var activeName = activeSheet.getName(); 
    if(activeName != 'Dropdowns'){  //Skip a hidden sheet used for validation on many of our spreadsheets 
     var activeId = activeSheet.getSheetId(); 
     var time = new Date(); 
     var fileName = time + " Backup: " + ssName + " " + activeName + ".csv"; 
     var csv = contentCSV(ssId,activeId); 
     var folder = DocsList.getFolderById(newFolderId); 
     folder.createFile(fileName, csv, 'text/plain'); 
    } 
    } 
    } catch(err) { 
    Logger.log(err) 
    } 
} 

function contentCSV(key,gid) { 
    var file = DocsList.getFileById(key); 
    var response =  UrlFetchApp.fetch("https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=" + key + 
       "&gid=" + gid + "&exportFormat=csv", oAuth()); 
    var fileText = response.getContentText(); 
    return fileText; 
} 

function oAuth() { 
    var oauthConfig = UrlFetchApp.addOAuthService("spreadsheets"); 
    var scope = "https://spreadsheets.google.com/feeds" 
    oauthConfig.setConsumerKey("anonymous"); 
    oauthConfig.setConsumerSecret("anonymous"); 
    oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope); 
    oauthConfig.setAuthorizationUrl("https://accounts.google.com/OAuthAuthorizeToken");  
    oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken"); 
var requestData = { 
    "oAuthServiceName": "spreadsheets", 
    "oAuthUseToken": "always", 
    }; 
    return requestData; 
} 

我該如何修改它以保存爲ods而不是csv?還是有更好的方法來保存備份?感謝您的幫助!

工作的代碼如下,由@嗶嘰-INSAS由於輸入:

注:爲了使這項工作,輸入備份文件夾的ID鍵,然後選擇ODS或通過註釋掉其他選擇XLSX。然後,您需要爲backUpMaker()設置定時小時觸發器。日誌會捕獲已暫時移動的文件的錯誤。這裏是:

function backUpMaker() { 
    var backupFolderId = '0B5--------------------1ZX1k'; 
    //var exportFormat = 'ods'; 
    var exportFormat = 'xlsx'; 
    var timeNow = new Date(); 
    var newFolder = DocsList.createFolder(timeNow); 
    var newFolderId =newFolder.getId(); 
    newFolder.addToFolder(DocsList.getFolderById(backupFolderId)); 
    newFolder.removeFromFolder(DocsList.getRootFolder()); 
    var sheets = DriveApp.getFilesByType(MimeType.GOOGLE_SHEETS); 
    while (sheets.hasNext()) { 
    var sheet = sheets.next(); 
    var lastUpdate = sheet.getLastUpdated(); 
    var timeHourAgo = new Date(); 
    timeHourAgo = timeNow - 3600000; 
    if(lastUpdate >= timeHourAgo){ 
     try{ 
     var key = sheet.getId(); 
     var name = sheet.getName(); 
     var fileName = timeNow + " Backup: " + name + "." + exportFormat; 
     var blob = contentBackup(key,exportFormat); 
     var folder = DocsList.getFolderById(newFolderId); 
     folder.createFile(blob).rename(fileName); 
     } catch(err){ 
     Logger.log(err); 
     } 
    } 
    } 
    var backupFolder = DocsList.getFolderById(backupFolderId); 
    try{ 
    backupFolder.createFile(Utilities.zip(newFolder.getFiles(), timeNow + '.zip')); 
    } catch(err) { 
    Logger.log(err); 
    } 
    newFolder.setTrashed(true); 
} 

function contentBackup(key,exportFormat) { 
    try{ 
    if(exportFormat == 'xlsx'){ 
     exportFormat = 'xls'; 
    } 
    var file = DocsList.getFileById(key); 
    var response =  UrlFetchApp.fetch("https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=" + key + 
        "&exportFormat=" + exportFormat, oAuth()); 
    var fileBlob = response.getBlob(); 
    return fileBlob; 
    } catch(err) { 
    Logger.log(err) 
    } 
} 

function oAuth() { 
    var oauthConfig = UrlFetchApp.addOAuthService("spreadsheets"); 
    var scope = "https://spreadsheets.google.com/feeds" 
    oauthConfig.setConsumerKey("anonymous"); 
    oauthConfig.setConsumerSecret("anonymous"); 
    oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope); 
    oauthConfig.setAuthorizationUrl("https://accounts.google.com/OAuthAuthorizeToken");  
    oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken"); 
var requestData = { 
    "oAuthServiceName": "spreadsheets", 
    "oAuthUseToken": "always", 
    }; 
    return requestData; 
} 
+0

SE有一個Web應用程序站點。 – pnuts

+0

爲什麼不啓用雲端硬盤離線訪問? –

+0

@ZigMandel,是的,我想到了,但我沒有訪問每個人的瀏覽器。奇怪這更容易。 – GASy

回答

0

你的代碼需要很少的變化來獲得ods文件。

2個區別:urlFetch中的請求格式變爲「ods」,返回的對象不再是字符串,而是一個blob。

除此之外,我保留了你寫的所有表單選擇函數,它看起來也很好。 (你做得很好!)。

以下是我從貴公司複製的完整代碼,並按照說明進行了更改。我的測試工作正常。

function backUpMaker() { 
    var backupFolderId = '0B3qS--------------TXdlY2M'; 
    var timeNow = new Date(); 
    var newFolder = DocsList.createFolder(timeNow); 
    var newFolderId =newFolder.getId(); 
    newFolder.addToFolder(DocsList.getFolderById(backupFolderId)); 
    newFolder.removeFromFolder(DocsList.getRootFolder()); 
    var sheets = DriveApp.getFilesByType(MimeType.GOOGLE_SHEETS); 
    while (sheets.hasNext()) { 
    var sheet = sheets.next(); 
    var lastUpdate = sheet.getLastUpdated(); 
    var timeHourAgo = new Date(); 
    timeHourAgo = timeNow - 3600000; 
    if(lastUpdate >= timeHourAgo){ 
     var sheetId = sheet.getId(); 
     var csv = eachSheet(sheetId,newFolderId); 
    } 
    } 
    var backupFolder = DocsList.getFolderById(backupFolderId); 
    try{ 
    backupFolder.createFile(Utilities.zip(newFolder.getFiles(), timeNow + '.zip')); 
    } catch(err) { 
    Logger.log(err); 
    } 
    newFolder.setTrashed(true); 
} 

function eachSheet(key,newFolderId) { 
    var ss = SpreadsheetApp.openById(key); 
    var ssId = ss.getId(); 
    var ssName = ss.getName(); 
    var howManySheets = ss.getNumSheets(); 
    try{ 
    for (var sheetIndex=0; sheetIndex < howManySheets; sheetIndex++) { 
    var activeSheet = ss.getSheets()[sheetIndex]; 
    var activeName = activeSheet.getName(); 
    if(activeName != 'Dropdowns'){  //Skip a hidden sheet used for validation on many of our spreadsheets 
     var activeId = activeSheet.getSheetId(); 
     var time = new Date(); 
     var fileName = time + " Backup: " + ssName + " " + activeName + ".ods"; 
     var blob = contentODS(ssId,activeId); 
     var folder = DocsList.getFolderById(newFolderId); 
     folder.createFile(blob).rename(fileName); 
    } 
    } 
    } catch(err) { 
    Logger.log(err) 
    } 
} 

function contentODS(key,gid) { 
    var file = DocsList.getFileById(key); 
    var response =  UrlFetchApp.fetch("https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=" + key + 
       "&gid=" + gid + "&exportFormat=ods", oAuth()); 
    var fileBlob = response.getBlob(); 
    return fileBlob; 
} 

function oAuth() { 
    var oauthConfig = UrlFetchApp.addOAuthService("spreadsheets"); 
    var scope = "https://spreadsheets.google.com/feeds" 
    oauthConfig.setConsumerKey("anonymous"); 
    oauthConfig.setConsumerSecret("anonymous"); 
    oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope); 
    oauthConfig.setAuthorizationUrl("https://accounts.google.com/OAuthAuthorizeToken");  
    oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken"); 
var requestData = { 
    "oAuthServiceName": "spreadsheets", 
    "oAuthUseToken": "always", 
    }; 
    return requestData; 
} 
+0

太棒了,謝謝。與csv版本不同,似乎使用ods我不再需要爲電子表格中的每個工作表創建單獨的文件,它會將整個電子表格保存在一個ods文件中。那是對的嗎? – GASy

+0

確實如此。 Ods是「全功能」格式...您也可以使用相同的代碼獲得xlsx格式,只需更改urlFetch調用即可。順便說一句,請考慮接受答案。謝謝 –

+0

接受的答案。我無法告訴你我是多麼高興,這是有效的。我會暫時在沒有表格選擇器的情況下發布代碼,否則你會得到重複的ods文件。那麼如果別人需要這樣的東西,你可以複製和粘貼。當我試圖創建斑點之前,我認爲我使它變得太複雜,無法讓它產生除PDF之外的任何東西。 – GASy

相關問題