【问题标题】:Google script xls attachment not displayingGoogle 脚本 xls 附件未显示
【发布时间】:2017-03-24 12:52:59
【问题描述】:

关于为什么此脚本通过电子邮件发送附件但附件不是正确的电子表格,并且看起来像是某种 google 错误页面的任何想法。

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("youremail@email.com", "Google Sheet to Excel", "The XLSX file is attached", {attachments: [blob]});
    
  } catch (f) {
    Logger.log(f.toString());
  }
}

【问题讨论】:

    标签: javascript google-apps-script google-api google-apps


    【解决方案1】:

    我猜 API 已经改变了。你可以试试Drive REST API(v3)。替换

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

    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();
    

    我进行了测试,它确实有效。当然,您首先应该在API Manager 获得您自己的 API 密钥等。然后,您可以在APIs Explorer 尝试一些 API,例如简单的 GET 请求。或者您可以尝试一些 API,在本例中为 Files: export,也可以在 the documentation page itself,但请注意,您不能在此处尝试自己的 API 密钥。

    【讨论】:

    • 我收到一个错误,它对我不起作用。 [17-03-24 11:51:17:145 CST] 异常:googleapis.com/drive/v3/files/MYFILEIDWASHERE/… 的请求失败返回代码 404。截断的服务器响应:{“错误”:{“错误”:[{“域”:“全局” , "reason": "notFound", "message": "找不到文件:MYFILEIDWASHERE...(使用 muteHttpExceptions 选项检查完整响应)
    • 我发现错误是由于电子表格的共享造成的——如果我向公众开放它就可以了。有没有办法让它不向公众开放?并且还在使用这个功能?
    • 对不起,我找不到相关信息。 也许您可以1)在导出之前将权限设置为公开,2)将其导出,3)然后将权限设置为其原始权限。 我认为 1) 和 3) 可以通过 Permissions API 完成。祝你好运。 :)
    • 这确实有效,感谢您的帮助!我将更新上面的代码以反映您的方法。
    【解决方案2】:

    这是@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);
    }

    【讨论】:

    • 很高兴你成功了! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多