2017-03-24 10 views
0

このスクリプトが添付ファイルを電子メールで送信しても添付ファイルが正しいスプレッドシートではないと思われ、Googleエラーページのように見えます。Googleスクリプトxls添付ファイルが表示されない

enter image description here

function getGoogleSpreadsheetAsExcel(){ 
 
    
 
    try { 
 
    
 
    var ss = SpreadsheetApp.getActive(); 
 
    
 
    var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + ss.getId() + "&exportFormat=xlsx"; 
 
    Logger.log(url); 
 
    
 
    var params = { 
 
     method  : "get", 
 
     headers  : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()}, 
 
     muteHttpExceptions: true 
 
    }; 
 
    
 
    var blob = UrlFetchApp.fetch(url, params).getBlob(); 
 
    
 
    blob.setName(ss.getName() + ".xlsx"); 
 
    
 
    MailApp.sendEmail("[email protected]", "Google Sheet to Excel", "The XLSX file is attached", {attachments: [blob]}); 
 
    
 
    } catch (f) { 
 
    Logger.log(f.toString()); 
 
    } 
 
}

答えて

1

私は、APIが変更されていると思います。代わりにDrive REST API(v3)を試すことができます。

var url = "https://www.googleapis.com/drive/v3/files/" + ss.getId() + 
    "/export?mimeType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&key=" + 
    "{your API key}"; 
var blob = UrlFetchApp.fetch(url).getBlob(); 

var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + ss.getId() + "&exportFormat=xlsx"; 
var params = { ... }; 
var blob = UrlFetchApp.fetch(url, params).getBlob(); 

を交換し、私はテストし、それが働きました。もちろん、まず自分自身のAPIキーなどをAPI Managerに取得する必要があります。次に、簡単なGETリクエストのようないくつかのAPIをAPIs Explorerで試すことができます。または、この場合はFiles: exportthe documentation page itselfでAPIを試すこともできますが、ここで独自のAPIキーを試すことはできません。

+0

。 [17-03-24 11:51:17:145 CST]例外:https://www.googleapis.com/drive/v3/files/MYFILEIDWASHERE/export?mimeType=application/vnd.openxmlformats-officedocumentでリクエストが失敗しました。 { "エラー":{ "エラー":[ { "ドメイン": "グローバル"、 "理由" "NOTFOUND"、 "メッセージ" spreadsheetml.sheet&キー= APIKEYWASHEREコード404切り捨てサーバの応答を返さ: "ファイルが見つかりません:MYFILEIDWASHERE ...(完全な応答を調べるためにmuteHttpExceptionsオプションを使用してください) – OblongMedulla

+0

私はエラーがスプレッドシートの共有によるものであることを認識しました - 私はそれを公開しています。 – OblongMedulla

+1

残念ながら私は関連情報を見つけることができません**おそらく**あなたは1)エクスポートする前にpublicに許可を設定し、2)エクスポートし、3)次にsその本来の許可に対する許可。 **私は** 1)と3)は[Permissions API](https://developers.google.com/drive/v3/reference/permissions)で行うことができると思います。がんばろう。 :) –

1

これはを手伝ってくれました@sangbok更新されたコードです:私はエラーが発生しますし、それは私のために働いていない

function getGoogleSpreadsheetAsExcel(){ 
 
    
 
    try { 
 
    
 
    var ss = SpreadsheetApp.getActive(); 
 
    var sheet = DriveApp.getFileById(ss.getId()); 
 
    
 
    // sets sharing to public - to send out email. 
 
    sheet.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT); 
 
    
 
    var url = "https://www.googleapis.com/drive/v3/files/" + ss.getId() + "/export?mimeType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&key=" + "YOURAPIKEYGOESHERE4"; 
 
    var blob = UrlFetchApp.fetch(url).getBlob(); 
 
    
 
    Logger.log(url); 
 
    
 
    blob.setName(ss.getName() + ".xlsx"); 
 
    
 
    var now = new Date(); 
 
    
 
    MailApp.sendEmail("YOUREMAILADDRESSGOESHERE", "EMAIL SUBJECT " + now , "EMAIL BODY " + now , {attachments: [blob]}); 
 
    
 
    } catch (f) { 
 
    Logger.log(f.toString()); 
 
    } 
 
    // returns the file back to Private access 
 
    sheet.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.EDIT); 
 
}

+1

あなたが作ってうれしい! :) –

関連する問題