I've developed a Google Form that generates a draft email in Gmail based on an HTML template. This process is controlled by a Google Apps Script, which currently works fine but has a limitation: the draft is always created in my Gmail account, regardless of who uses the form.
What I intended was for this form to be usable by others in such a way that when they submit the form, the draft is created in their own Gmail account, not mine. I'm currently facing challenges in implementing this functionality.
Does anyone have experience or insights on how I can modify my script to allow the draft to be created in the Gmail account of the user who submits the form, rather than always in my account? Any guidance or suggestions would be greatly appreciated!
function createDraft(headerContent,replyToEmail) {
var officeEmailTemplate = getHTMLFromDrive();
officeEmailTemplate = officeEmailTemplate.replace('$$$BODY_REPLACE', "<enter message body here>");
officeEmailTemplate = officeEmailTemplate.replace('$$$TITLE_REPLACE', headerContent);
GmailApp.createDraft('', '', '', {
//attachments: [file.getAs(MimeType.PDF)],
htmlBody: officeEmailTemplate,
replyTo: replyToEmail
});
}
function getHTMLFromDrive() {
var fileId = 'my-google-drive-template-ID'; // Replace with the actual file ID
var file = DriveApp.getFileById(fileId);
var htmlContent = file.getBlob().getDataAsString();
return htmlContent;
}
function onFormSubmit(e) {
var formResponses = e.response.getItemResponses();
var headerContent = '';
var replyToEmail = '';
for (var i = 0; i < formResponses.length; i++) {
var itemResponse = formResponses[i];
var questionTitle = itemResponse.getItem().getTitle();
if (questionTitle === 'Header content') {
headerContent = itemResponse.getResponse();
} else if (questionTitle === 'Reply to email address') {
replyToEmail = itemResponse.getResponse();
}
}
createDraft (headerContent,replyToEmail)
}
If in your situation, it seems that your Google Apps Script is run with the OnSubmit trigger by submitting Google Form. In this case, the script is run as the owner of Google Form. I guessed that this might be the reason for your current issue.
If you want to make users run the script as each user, I would like to propose using HTML form with Web Apps instead of Google Form. By this, the script can be run as each user.
From is there any sample you could provide for this?
, in order to achieve your goal, how about the following answer?
Please create a standalone script and open the script editor.
Please copy and paste the following sample script to the script editor.
Code.gs
function doGet() {
return HtmlService.createHtmlOutputFromFile("index");
}
function getHTMLFromDrive() {
var fileId = 'my-google-drive-template-ID'; // Replace with the actual file ID
var file = DriveApp.getFileById(fileId);
var htmlContent = file.getBlob().getDataAsString();
return htmlContent;
}
function createDraft({ headerContent, replyToEmail }) {
var officeEmailTemplate = getHTMLFromDrive();
officeEmailTemplate = officeEmailTemplate.replace('$$$BODY_REPLACE', "<enter message body here>");
officeEmailTemplate = officeEmailTemplate.replace('$$$TITLE_REPLACE', headerContent);
GmailApp.createDraft('', '', '', {
//attachments: [file.getAs(MimeType.PDF)],
htmlBody: officeEmailTemplate,
replyTo: replyToEmail
});
return "done";
}
index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<form>
<label for="headerContent">Header content</label>
<input type="text" name="headerContent" id="headerContent">
<label for="replyToEmail">Reply to email address</label>
<input type="email" name="replyToEmail" id="replyToEmail">
<input type="submit" value="submit", onclick="google.script.run.withSuccessHandler(e => console.log(e)).createDraft(this.parentNode); return false;">
</form>
</body>
</html>
In order to use the file on your Google Drive from the users, please share the file of var fileId = 'my-google-drive-template-ID';
with the users as the viewer. Of course, you can publicly share it. If you don't want to share it, please hard copy the text data as htmlContent
.
The detailed information can be seen in the official document.
Please set this using the script editor.
https://script.google.com/macros/s/###/exec
.Please open the Web Apps URL with the browser of another user account. By this, it opens a dialog for authorizing the scopes. And, when you permit the scopes, the script is run. You can see 2 questions. When you input 2 answers and click a submit button, the values are sent to the Google Apps Script side and a draft email is created in Gmail for each user.
This is a simple sample script. So, please modify this to your actual situation.
When you modify the Google Apps Script of Web Apps, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful about this.
You can see the details of this in my report "Redeploying Web Apps without Changing URL of Web Apps for new IDE (Author: me)".