如何将Google文档文件附加为Microsoft Word文档。

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

How to attach a google doc file as Microsoft Word doc

问题

My first time here in Stack overflow. I'm just an amateur programmer trying to learn Google Apps script. I created a google form to get answers from users. I have transferred the answers to a google doc formatted according to our use in the office. I need to email the google doc back to the for user in Microsoft Word format. Sending as PDF is not a problem, but MS Word is a bit tricky.

I tried copying the recommendations I found here, but I can't seem to get it to run. I hope someone can tell me what I'm doing wrong and what I need to do. Here's the relevant portion of the code:

doc.saveAndClose(); // end of the transfer from google sheet to google doc. now to email...

var filename = ts + "-" + c_name1;

var compfile = DriveApp.getFilesByName(filename);

if (!compfile.hasNext()) {
  console.error("Could not open file " + filename);
  return;
}

var template = HtmlService.createTemplateFromFile('email_message');

var message = template.evaluate().getContent();

MailApp.sendEmail({
  to: em,
  subject: "Automated Complaint Assistant",
  htmlBody: message,
  attachments: compfile.next()
})  // attachment is PDF file type. HOW TO SEND AS WORD? 

I tried this function I found here:

function EmailCompAsWordDoc(email, name, newCopyID) {

  var subject = "Word version of Complaint";
  var body = "\n\nHere is the Microsoft Word version of the draft Complaint";
  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();

  // Create the DOCX file in my TEMP folder in Google Drive and send
  var file = DriveApp.createFile(doc).setName('Draft Complaint ' + name + '.docx');
  DriveApp.getFolderById('<ID of my TEMP folder in Google Drive>').addFile(file);
  var blob = DriveApp.getFileById(file.getId());

  if (MailApp.getRemainingDailyQuota() > 0) {
    GmailApp.sendEmail(email, subject, body, {
      Body: body,
      attachments: [blob]
    });
  }

  // Remove the DOCX file from the TEMP folder
  DriveApp.getFileById(file.getId()).setTrashed(true);
}

var compid = DriveApp.getFileById(filename.getId()) // error here. How to get the file ID for use by this custom function??

EmailCompAsWordDoc(em, c_name1, compid)

Please note that the code you provided has some issues. For example, DriveApp.getFileById(filename.getId()) doesn't seem to be correctly defined, and it may not work as intended. You might need to make some modifications to the code to resolve these issues.

英文:

My first time here in Stack overflow. I'm just an amateur programmer trying to learn Google Apps script. I created a google form to get answers from users. I have transferred the answers to a google doc formatted according to our use in the office. I need to email the google doc back to the for user in microsoft word format. Sending as pdf is not a problem, but msword is a bit tricky.

I tried copying the recommendations i found here but I can't seem to get it to run. I hope someone can tell me what i'm doing wrong and what i need to doe. here's the relevant portion of the code:

`doc.saveAndClose(); // end of the transfer from google sheet to google doc. now to email...

  var filename= ts+&quot;-&quot;+c_name1;

  var compfile = DriveApp.getFilesByName(filename);

  if (!compfile.hasNext()) 
  {
    console.error(&quot;Could not open file &quot;+filename);
    return;
  }

  var template = HtmlService.createTemplateFromFile(&#39;email_message&#39;);

  var message = template.evaluate().getContent();


   MailApp.sendEmail({
    to:em, 
    subject: &quot;Automated Complaint Assistant&quot;, 
    htmlBody: message,
    attachments: compfile.next()})  // attachment is pdf file type. HOW TO SEND AS WORD? 

`

I tried this function i found here:

`function EmailCompAsWordDoc(email, name, newCopyID) {

  var subject = &quot;Word version of Complaint&quot;; 
  var body = &quot;\n\nHere is the Microsoft Word version of the draft Complaint&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;Draft Complaint &#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);
}


var compid = DriveApp.getFileById(filename.getId()) // error here. How to get the file ID for use by this custom function??

EmailCompAsWordDoc(em, c_name1, compid)

`

答案1

得分: 0

从您提供的脚本中,我理解到filename的值是var filename = ts + &quot;-&quot; + c_name1;的字符串值。我认为这就是导致您当前遇到filename.getId is not a function问题的原因。

在这种情况下,考虑以下修改:

从:

var compid = DriveApp.getFileById(filename.getId())

到:

var compid = DriveApp.getFilesByName(filename).next().getId();
  • 在这种情况下,假设filenameemc_name1的值在EmailCompAsWordDoc(em, c_name1, compid)中是有效的值。请注意这一点。
英文:

From your provided script, I understood that the value of filename is the string value of var filename = ts + &quot;-&quot; + c_name1;. I thought that this is the reason for your current issue of filename.getId is not a function.

In this case, how about the following modification?

From:

var compid = DriveApp.getFileById(filename.getId())

To:

var compid = DriveApp.getFilesByName(filename).next().getId();
  • In this case, it supposes that the values of filename and em and c_name1 of EmailCompAsWordDoc(em, c_name1, compid) are valid values. Please be careful about this.

huangapple
  • 本文由 发表于 2023年2月6日 14:53:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358175.html
匿名

发表评论

匿名网友

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

确定