Google App Script - callback from Mail body

2019-08-29 11:11发布

问题:

I am sending an html form in body of the mail with a button included and when button is clicked i am saving a reference in google spreadsheet. I was able to get the code and not able to get it worked.please help me in fixing the issue.

//Function to send mail from Google App script  
function sendmail(e) {  
var subject = 'Test';  

 var template = HtmlService.createHtmlOutput('<form id = "myForm" >' +  
'<label for = "name"> Name: </label>'+  
'<input type = "text" id ="name" />'+  
'</Form>'+  
'<button onclick = "submitdata()">Save</button>' +  
'<script>' +  
'function submitdata(){'+  
'var form =  document.getElementByID('+"myform"+');'+  
'google.script.run.withFailureHandler(alert).withSuccessHandler(alert).' +  
'submitForm(form);'+  
 '}');  
var html = template.getContent();  
// email to self  
recipient = Session.getActiveUser().getEmail();  
var Mailbody= 'Its a test: '  
// Send email form  
GmailApp.sendEmail(recipient, subject, Mailbody, {htmlBody:html} );  
}


//Function to be called when clicked on Save button in Email body

function submitForm(form) {  
var ss =SpreadsheetApp.openById('tsgxsbtJRf2zbh_tNQzJoUQ');  
var sheet = ss.getSheets()[0];  
// Save response to spreadsheet  
var rowNum = sheet.getLastRow()+1;  
//sheet.getRange(rowNum, 1, 1, row.length).setValues([row]);  
sheet.getRange("A"+ rowNum).setValue("today" )  
}

回答1:

No email client that I know of, traditional (e.g. outlook) or online (e.g. gmail) will run javascript code embedded in html.

What work on some clients is to have the form with a regular post URL, so that when the user fill the fields and click the submit button, it'll trigger a new tab on their browser posting the form directly. But then again, this only work on some email clients. For example, I don't think this works on Outlook.

In such cases, the best you can do is to send a link so the user can open our form online.