英文:
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+"-"+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)
`
答案1
得分: 0
从您提供的脚本中,我理解到filename
的值是var filename = ts + "-" + c_name1;
的字符串值。我认为这就是导致您当前遇到filename.getId is not a function
问题的原因。
在这种情况下,考虑以下修改:
从:
var compid = DriveApp.getFileById(filename.getId())
到:
var compid = DriveApp.getFilesByName(filename).next().getId();
- 在这种情况下,假设
filename
、em
和c_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 + "-" + 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
andem
andc_name1
ofEmailCompAsWordDoc(em, c_name1, compid)
are valid values. Please be careful about this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论