使用Google脚本文件导出更改doc的名称

时间:2017-04-09 19:08:04

标签: google-apps-script

我发送了一个google doc作为电子邮件中的附件,但该附件工作正常,但附件名为" export.docx'我无法弄清楚如何改变它。我正在使用此API - https://developers.google.com/drive/v2/reference/files/export

我的代码如下。如果有人可以帮我改变附加文件的名称那就太棒了!谢谢!

function run(id, callback) {
  var service = getDriveService();
  if (service.hasAccess()) {
    var token = service.getAccessToken();
    var response = UrlFetchApp.fetch('https://www.googleapis.com/drive/v2/files/' + id + '/export?mimeType=application/vnd.openxmlformats-officedocument.wordprocessingml.document', {
      headers: {
        Authorization: 'Bearer ' + token
      }
    });
    MailApp.sendEmail({
      to: email,
      subject: "Attachments",
      htmlBody: "Please see attached.",
      attachments: [response]
    });
  } else {
    Logger.log("Access denied")
  }
}

1 个答案:

答案 0 :(得分:1)

您这样做是为了重命名名称:

function run(id, callback) {
  var service = getDriveService();
  if (service.hasAccess()) {
    var token = service.getAccessToken();
    var response = UrlFetchApp.fetch('https://www.googleapis.com/drive/v2/files/' + id + '/export?mimeType=application/vnd.openxmlformats-officedocument.wordprocessingml.document', {
      headers: {
        Authorization: 'Bearer ' + token
      }
    });
    MailApp.sendEmail({
      to: email,
      subject: "Attachments",
      htmlBody: "Please see attached.",
      attachments: [response.getBlob().setName("NewName")]  
    });
  } else {
    Logger.log("Access denied")
  }
}

基本上,使用setName函数将响应转换为要使用的blob和setnewName

希望有所帮助