2014-09-10 75 views
1

讓我先說我意識到這個問題之前已經被問過,所以真誠的道歉再次提出。但是,我已經嘗試了一些建議,但無法解決問題。我正在嘗試編寫一個腳本,它將採用Google Spreadsheet,將其轉換爲Excel XLS/XLSX格式,然後將轉換後的文件作爲附件發送給您。這是試圖創建轉換文件的代碼。使用腳本將Google Spreadsheet轉換爲Excel XLS

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

function test(){ 
var id = DocsList.getFileById('FILE_ID_HERE'); 
var url = 'https://docs.google.com/feeds/'; 
var doc = UrlFetchApp.fetch(url+'download/spreadsheets/Export?key='+id+'&exportFormat=xls', 
googleOAuth_('docs',url)).getBlob() 
DocsList.createFile(doc).rename('newfile.xls') 
} 

function autorise(){ 
    // function to call to authorize googleOauth 
    var id = SpreadsheetApp.getActiveSpreadsheet().getId() 
    var url = 'docs.google.com/feeds/'; 
    var doc = UrlFetchApp.fetch(url+'download/documents/Export?exportFormat=html&format=html&id='+id, googleOAuth_('docs',url)).getContentText(); 
} 

我讀過授權程序是主要的罪魁禍首。我已授予授權,它似乎運行到下面的行然後出現錯誤。

var doc = UrlFetchApp.fetch(url+'download/spreadsheets/Export?key='+id+'&exportFormat=xls', 
googleOAuth_('docs',url)).getBlob() 

該錯誤消息指出以下幾點:

Request failed for returned code 404. Truncated server response: 
<!DOCTYPE html><html lang="en" ><head><meta name="description" content="Web word 
processing, presentations and spreadsheets"><link rel="shortcut ic... 
(use muteHttpExceptions option to examine full response) (line 50, file "Code") 

如果任何人有任何想法,我將不勝感激。

+0

下面是用了最新的答案類似的問題。 http://stackoverflow.com/questions/31809987/google-app-scripts-email-a-spreadsheet-as-excel – 2015-10-08 15:06:13

回答