1
votes

I sent a mail via google script which sends button in email body, I want to update a spreadsheet when button in email body is clicked.I am unable to trigger the function present in script.

myfunction1 is used to send the email with button in body.

function myfunction1() {
var htmlBody = HtmlService.createHtmlOutputFromFile('mail_template').getContent();
MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: 'Testing Gmail action:',
htmlBody: htmlBody,});}

body in mail_template

<html>
<body>
<p id="demo"></p>
<div>EMAIL CONTENT GOES HERE<br/></div>
<form>
<input type="button" id='mybtn' onclick="google.script.host.updatesheet()"       value ="click me"/>
</form>
</body>
</html>

updatesheet is the function used to update the spreadsheet.

function updatesheet()
{
var spreadsheetkey = "1q2hgrZZCR1ytQEX-irUQWjXg5g6YNaRwcfsTQq7UZ30";
var sheet = SpreadsheetApp.openById(spreadsheetkey).getActiveSheet();
var lastrow = sheet.getLastRow();
sheet.getRange(lastrow+1, 1).setValue("testing");
}
1

1 Answers

0
votes

you are calling google.script.host.updatesheet from the form in the email, which will be fully outside of the apps script, thus the browser has no idea where that function is. if you debug the click to the button you would have seen browser errors.

look at the docs for building a content service. your button form must post to that service url.