以Word格式发送附件在Google脚本中无法正常工作。

huangapple go评论91阅读模式
英文:

Sending attachment in Word format does not work in Google Script

问题

我已经成功发送了 PDF 格式的附件,但是当我尝试发送 Word 格式的附件时,出现了以下错误:

"Request failed for https://docs.google.com returned code 404. Truncated server response: <html".

我尝试了这个网站上建议的几种方法,但都无法使其工作。

非常感谢您对这个主题的任何帮助。我是 Google Script 的新手。

这是我的代码:

//=================================================
// 以 Word 格式发送就业信函,而不是 PDF
//=================================================

function EmailCertAsWordDoc(requestor, name, newCopyID) {
  var email = requestor; 
  var id = DocumentApp.openById(newCopyID);
  var subject = "自动生成的就业信函"; 
  var body = "\n\n附件是您为" + name + "创建的就业信函。\n\n\n\n\n ";
  
  var url = "https://docs.google.com/feeds/download/documents/export/Export?id=" + id + "&format=docx&access_token=" + ScriptApp.getOAuthToken();
  var docx = UrlFetchApp.fetch(url).getBlob();
  
  if (MailApp.getRemainingDailyQuota() > 0) 
    GmailApp.sendEmail(email, subject, {
      Body: body,
      attachments:[docx]     
    });
}
英文:

I've been able to send attachment in PDF format successfully but when I try to send attachment in Word format, I get this error:

"Request failed for https://docs.google.com returned code 404. Truncated server response: <!DOCTYPE html><html".

I've tried several ways suggested in this site but I just couldn't make it work.

Will very much appreciate any help on this topic. I'm a newbie in Google Script.

Here's my code:

//=================================================
//  Email Employment Letter as Word format, not PDF
//=================================================

function EmailCertAsWordDoc(requestor, name, newCopyID) {
  var email = requestor; 
  var id = DocumentApp.openById(newCopyID);
  var subject = &quot;Auto-generated Employment Letter&quot;; 
  var body = &quot;\n\nAttached is the Employment Letter that you created for &quot; + name + &quot;. \n\n\n\n\n &quot;;
  
  var url = &quot;https://docs.google.com/feeds/download/documents/export/Export?id=&quot; + id + &quot;&amp;format=docx&amp;access_token=&quot; + ScriptApp.getOAuthToken();
  var docx = UrlFetchApp.fetch(url).getBlob();
  
  if (MailApp.getRemainingDailyQuota() &gt; 0) 
    GmailApp.sendEmail(email, subject, {
      Body: body,
      attachments:[docx]     
    });
}

答案1

得分: 1

以下是翻译好的部分:

  1. 要获取导出链接,您需要使用文件 ID,而不是文件本身 - 在您的情况下,它是 newCopyID

  2. 不幸的是,将 Word 文件作为附件发送比 PDF 复杂一些。为了使其工作,您可以在您的驱动器上创建一个 Word 文件,然后将其作为 URL 放在电子邮件正文中。

示例:

function EmailCertAsWordDoc(requestor, name, newCopyID) {
  var email = requestor; 
  var fileName = DocumentApp.openById(newCopyID).getName();
  var subject = "自动生成的雇佣信"; 
  var body = "\n\n附上了您为 " + name + " 创建的雇佣信。\n\n\n\n\n ";

  var url = "https://docs.google.com/feeds/download/documents/export/Export?id=" + newCopyID + "&format=docx&access_token=" + ScriptApp.getOAuthToken();
  var docx = UrlFetchApp.fetch(url).getBlob();
  var file = DriveApp.createFile(docx).setName(fileName).getUrl();
  body += " " + file;
  if (MailApp.getRemainingDailyQuota() > 0) 
    GmailApp.sendEmail(email, subject, body);
}

请注意,我只翻译了代码中的注释和字符串部分。

英文:

Two things:

  1. To obtain the export url, you need to use the file id, no the file itself - in your case it is newCopyID

  2. Sending word files as attachments is unfortunately a little bit more complicated than pdfs. To make it work, you can create a word file on your drive, and then send it as an URL within your email body.

Sample:

function EmailCertAsWordDoc(requestor, name, newCopyID) {
  var email = requestor; 
  var fileName = DocumentApp.openById(newCopyID).getName();
  var subject = &quot;Auto-generated Employment Letter&quot;; 
  var body = &quot;\n\nAttached is the Employment Letter that you created for &quot; + name + &quot;. \n\n\n\n\n &quot;;

  var url = &quot;https://docs.google.com/feeds/download/documents/export/Export?id=&quot; + newCopyID + &quot;&amp;format=docx&amp;access_token=&quot; + ScriptApp.getOAuthToken();
  var docx = UrlFetchApp.fetch(url).getBlob();
  var file=DriveApp.createFile(docx).setName(fileName).getUrl();
  body+=&quot; &quot;+file;
  if (MailApp.getRemainingDailyQuota() &gt; 0) 
    GmailApp.sendEmail(email, subject, body);
}

答案2

得分: 0

这是我在遵循ziganotschka的建议后的最终工作代码:

//=================================================
//  将就业信以Word格式发送,而不是PDF
//=================================================

function EmailCertAsWordDoc(email, name, newCopyID) {

  var subject = "自动生成的就业信"; 
  var body = "\n\n附上了您为" + name + "创建的就业信。\n\n\n\n\n";  
  var url = 'https://docs.google.com/feeds/download/documents/export/Export?id=' + newCopyID + '&exportFormat=docx';
  var options = {
      headers: {
      Authorization: "Bearer " + ScriptApp.getOAuthToken()
      },
      muteHttpExceptions: true
      }
  var response = UrlFetchApp.fetch(url, options);
  var doc = response.getBlob();
  
  // 在我的Google Drive的TEMP文件夹中创建docx文件并发送
  var file = DriveApp.createFile(doc).setName('就业信 - '+ name + '.docx');
  DriveApp.getFolderById('<在Google Drive中我的TEMP文件夹的ID>').addFile(file);   
  var blob = DriveApp.getFileById(file.getId());
  
  if (MailApp.getRemainingDailyQuota() > 0) 
    GmailApp.sendEmail(email, subject, body, {
      Body: body,
      attachments:[blob]     
    });  
  
  // 从TEMP文件夹中删除docx文件
  DriveApp.getFileById(file.getId()).setTrashed(true);
}
英文:

Here's my final working code after following ziganotschka's advice:

//=================================================
//  Email Employment Letter as Word format, not PDF
//=================================================

function EmailCertAsWordDoc(email, name, newCopyID) {

  var subject = &quot;Auto-generated Employment Letter&quot;; 
  var body = &quot;\n\nAttached is the Employment Letter that you created for &quot; + name + &quot;. \n\n\n\n\n &quot;;  
  var url = &#39;https://docs.google.com/feeds/download/documents/export/Export?id=&#39; + newCopyID + &#39;&amp;exportFormat=docx&#39;;
  var options = {
      headers: {
      Authorization: &quot;Bearer &quot; + ScriptApp.getOAuthToken()
      },
      muteHttpExceptions: true
      }
  var response = UrlFetchApp.fetch(url, options);
  var doc = response.getBlob();
  
  //Create the docx file in my TEMP folder in Google Drive and send
  var file = DriveApp.createFile(doc).setName(&#39;Employment Letter - &#39;+ name + &#39;.docx&#39;);
  DriveApp.getFolderById(&#39;&lt;ID of my TEMP folder in Google Drive&gt;&#39;).addFile(file);   
  var blob = DriveApp.getFileById(file.getId());
  
  if (MailApp.getRemainingDailyQuota() &gt; 0) 
    GmailApp.sendEmail(email, subject, body, {
      Body: body,
      attachments:[blob]     
    });  
  
  //Remove the docx file from the TEMP folder
  DriveApp.getFileById(file.getId()).setTrashed(true);
}

huangapple
  • 本文由 发表于 2020年1月6日 17:33:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定