我创建了一个网络应用,可以从自定义表单中提取信息并将其放入电子表格中。来自电子表格的信息与文档模板一起用于进行邮件合并,然后将其作为pdf发送。我想将文件作为word文档而不是pdf发送。我已经访问过Send attachement as Word document via Google Script并且可以将其作为一个独立的脚本运行,但我想让它与我的Web应用程序一起使用。我在哪里将它放在代码中以使其工作?我尝试进入多个地方,无论我放在哪里,它都会使网络应用程序失败。任何帮助将不胜感激。 这是代码
function doGet(e) {
var app = UiApp.createApplication().setTitle('Bulletin Creator');
var panel = app.createFormPanel();
var grid = app.createGrid(13,2).setId('BulletinGrid');
var bulletinNameLabel = app.createLabel("Bulletin #");
var bulletinTextbox = app.createTextBox().setWidth('150px').setName('bulletin');
var preparedDateLabel = app.createLabel('Date Prepared');
var preparedDateTextbox = app.createDateBox().setWidth('150px').setName('dateprepared');
var releasedDateLabel = app.createLabel('Date Released');
var releasedDateTextbox = app.createDateBox().setWidth('150px').setName('datereleased');
var preparedByLabel = app.createLabel('Prepared By');
var preparedBy = app.createListBox().setWidth('150px').setName('preparedby');
preparedBy.addItem('Select Option');
preparedBy.addItem(', Officer, Intelligence Unit');
preparedBy.addItem(', Sergeant, Intelligence Unit');
preparedBy.addItem('As many');
preparedBy.addItem('other names');
preparedBy.addItem('as you need');
var reviewedByLabel = app.createLabel('Reviewed By');
var reviewedBy = app.createListBox().setWidth('150px').setName('reviewedby');
reviewedBy.addItem('Select Option');
reviewedBy.addItem('Officer, Intelligence Unit');
reviewedBy.addItem('Intelligence Unit');
reviewedBy.addItem('As many');
reviewedBy.addItem('other names');
reviewedBy.addItem('as you need');
var subjectInterestLabel = app.createLabel('Subject of Interest');
var subjectInterestTextbox = app.createTextBox().setWidth('150px').setName('subjectinterest');
var criminalOffenseLabel = app.createLabel('Criminal Offense');
var criminalOffenseTextbox = app.createTextBox().setWidth('150px').setName('criminaloffense');
var physicalDescriptionLabel = app.createLabel('Physical Description');
var physicalDescriptionParagraphbox = app.createTextArea().setSize('150px', '50px').setName('physicaldescription');
var addressLabel = app.createLabel('Address');
var addressTextbox = app.createTextBox().setWidth('150px').setName('address');
var arrestLocationLabel = app.createLabel('Arrest Location');
var arrestLocationTextbox = app.createTextBox().setWidth('150px').setName('arrestlocation');
var suggestionsLabel = app.createLabel('Suggestions and Recommendations');
var suggestionsParagraphbox = app.createTextArea().setSize('150px', '50px').setName('suggestions');
//file upload
var upLoadTypeLabel = app.createLabel('Image Upload'); //Image upload Text
var upLoad = (app.createFileUpload().setName('thefile')); //Choose file button
var submitButton = app.createSubmitButton('<B>Submit</B>'); //submit button
var warning = app.createHTML('<B>PLEASE WAIT WHILE DATA IS UPLOADING<B>').setStyleAttribute('background','yellow').setVisible(false)
//Grid layout of items on form
grid.setWidget(0, 0, bulletinNameLabel)
.setWidget(0, 1, bulletinTextbox)
.setWidget(1, 0, preparedDateLabel)
.setWidget(1, 1, preparedDateTextbox)
.setWidget(2, 0, releasedDateLabel)
.setWidget(2, 1, releasedDateTextbox)
.setWidget(3, 0, preparedByLabel)
.setWidget(3, 1, preparedBy)
.setWidget(4, 0, reviewedByLabel)
.setWidget(4, 1, reviewedBy)
.setWidget(5, 0, subjectInterestLabel)
.setWidget(5, 1, subjectInterestTextbox)
.setWidget(6, 0, criminalOffenseLabel)
.setWidget(6, 1, criminalOffenseTextbox)
.setWidget(7, 0, physicalDescriptionLabel)
.setWidget(7, 1, physicalDescriptionParagraphbox)
.setWidget(8, 0, addressLabel)
.setWidget(8, 1, addressTextbox)
.setWidget(9, 0, arrestLocationLabel)
.setWidget(9, 1, arrestLocationTextbox)
.setWidget(10, 0, suggestionsLabel)
.setWidget(10, 1, suggestionsParagraphbox)
.setWidget(11, 0, upLoadTypeLabel)
.setWidget(11, 1, upLoad)
.setWidget(12, 0, submitButton)
.setWidget(12, 1, warning)
var cliHandler = app.createClientHandler().forTargets(warning).setVisible(true)
submitButton.addClickHandler(cliHandler);
panel.add(grid);
app.add(panel);
return app;
}
function doPost(e) {
var app = UiApp.getActiveApplication();
var bulletin = e.parameter.bulletin; //pass submitted bulletin data through to bulletin variable
var dateprepared = e.parameter.dateprepared;
var datereleased = e.parameter.datereleased;
var preparedby = e.parameter.preparedby;
var reviewedby = e.parameter.reviewedby;
var subjectinterest = e.parameter.subjectinterest;
var criminaloffense = e.parameter.criminaloffense;
var physicaldescription = e.parameter.physicaldescription;
var address = e.parameter.address;
var arrestlocation = e.parameter.arrestlocation;
var suggestions = e.parameter.suggestions;
var fileBlob = e.parameter.thefile; // data returned is a blob for FileUpload widget
var sheet = SpreadsheetApp.openById(submissionSSKey).getActiveSheet();
var lastRow = sheet.getLastRow();
var lastCol = sheet.getLastColumn();
var targetRange = sheet.getRange(lastRow+1, 1, 1, 11).setValues([[bulletin,dateprepared,datereleased,preparedby,reviewedby,subjectinterest,criminaloffense,physicaldescription,address,arrestlocation,suggestions]]);
var copyId = DocsList.getFileById(docTemplate).makeCopy(docName + bulletin).getId();
var Doc = DocumentApp.openById(copyId); // Open the new document
var Body = Doc.getActiveSection(); // Get the document’s body section
Body.replaceText('kbulletin', bulletin);
Body.replaceText('kprepareddate', dateprepared);
Body.replaceText('kreleaseddate', datereleased);
Body.replaceText('kpreparedby', preparedby);
Body.replaceText('kreviewedby', reviewedby);
Body.replaceText('ksubjectinterest', subjectinterest);
Body.replaceText('kcriminaloffense', criminaloffense);
Body.replaceText('kphysicaldescription', physicaldescription);
Body.replaceText('kaddress', address);
Body.replaceText('karrestlocation', arrestlocation);
Body.replaceText('ksuggestions', suggestions);
var totalElements = Doc.getNumChildren();
var el=[]
for( var j = 0; j < totalElements; ++j ) {
var element = Doc.getChild(j);
var type = element.getType();
if (type =='PARAGRAPH'){
el[j]=element.getText()
if(el[j]=='###'){
element.removeFromParent();// remove the ###
Doc.insertImage(j, fileBlob).setWidth(150).setHeight(200);// 'image' is the image file as blob
}
}
}
Doc.saveAndClose(); // Save and close the temporary document
var targetfolder = DocsList.getFolderById(bulletinfolderdrop);
var newfile = DocsList.getFileById(copyId);
newfile.addToFolder(targetfolder); //Adds file to specified folder
newfile.removeFromFolder(DocsList.getRootFolder()); //Remove the copy left in the root, leaving only the version in the specific folder
var pdf = DocsList.getFileById(copyId).getAs("application/pdf"); //get file as pdf
var subject = "General Intelligence Bulletin # " + bulletin; //Email subject line
var body = "Attached is bulletin # " + bulletin + " for " + subjectinterest; //Email Body
MailApp.sendEmail({
to: email_address,
//cc: email_address,
subject: subject,
htmlBody: body,
attachments: pdf,});
var label = app.createLabel('Bulletin # ' + bulletin + ' uploaded successfully');
app.add(label);
return app
}
以下是我尝试合并的代码,以便将附件作为word文档发送。
function emailDocTestasDocx() {
var id = '1I9KIVTLieQbNnmz09zfOBSBNwZ9Tp7B0kfpysaf-ooY';// an example of Google doc
var url = 'https://docs.google.com/feeds/';
var doc = UrlFetchApp.fetch(url+'download/documents/Export?exportFormat=doc&format=doc&id='+id,
googleOAuth_('docs',url)).getBlob();
var me = Session.getEffectiveUser().getEmail();
MailApp.sendEmail(me, 'test', 'see attachment', {attachments:[doc]});
}
function googleOAuth_(name,scope) {
var oAuthConfig = UrlFetchApp.addOAuthService(name);
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setConsumerKey('anonymous');
oAuthConfig.setConsumerSecret('anonymous');
return {oAuthServiceName:name, oAuthUseToken:"always"};
}
答案 0 :(得分:0)
我认为问题在于电子邮件结构的语法:
这是应该如何构建的,来自Google文档:
// Send an email with two attachments: a file from Google Drive (as a PDF) and an HTML file.
var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
var blob = Utilities.newBlob('Insert any HTML content here', 'text/html', 'my_document.html');
MailApp.sendEmail('mike@example.com', 'Attachment example', 'Two files are attached.', {
name: 'Automatic Emailer Script',
attachments: [file.getAs(MimeType.PDF), blob]
});
该示例是发送两个附件。您只想发送一个文件。请注意,大括号{}
内的所有内容都是对象。该对象有两个属性name:
和attachments:
。附件属性具有属性值的数组。 JavaScript中的数组位于括号[]
中。数组只是一个用逗号分隔的元素列表。
数组的结构很简单,[element1, element2, element3, etc]
,但对象的结构可能要复杂得多。
要发送包含附件的电子邮件,必须使用以下语法:
sendEmail(recipient, subject, body, options)
options
是一个对象。因为它是一个对象,所以任何东西,无论多么简单或复杂,都必须用括号{}包围。
要了解JavaScript中的对象,您可以查看以下链接:JSON
我猜测唯一需要的高级参数是attachments
参数。