I have created the following gmail add-on and am looking for some guidance. The goal is to enable the inbox owner to select several mails and package them into a zip file. The owner can then download or drag and drop the URL to another application. I am facing two problems. The add-on view seems to only be additive for each new email selected. Selecting a previously selected email only shows buttons for emails / files that were selected and created prior. Also, once a file has been removed, it cant be added again. Its as if each mail has its own view or cache of the add-on and I cant figure out how to refresh the view during each event or mail selection.
Also, in addition to the zip button, I want to have a html text view of the URL in the card but cant figure out the refresh sequence to show it, it is currently commented out.
// Function to load the Gmail add-on
function loadAddOn(event) {
var url = ""
if (event) {
var accessToken = event.gmail.accessToken;
var messageId = event.gmail.messageId;
GmailApp.setCurrentMessageAccessToken(accessToken);
var mailMessage = GmailApp.getMessageById(messageId);
var eml = mailMessage.getRawContent();
var date = new Date().getTime(); // Get current epoch time
// Create or get the folder where emails will be stored
var tempFolder;
try {
tempFolder = DriveApp.getFoldersByName('temp').next();
} catch (e) {
tempFolder = DriveApp.createFolder('temp');
}
// Create a file for the current email in the folder with a unique name
var messageIdSuffix = messageId.split(':').pop(); // Get the part after the colon
var fileName = mailMessage.getSubject() + '_' + messageIdSuffix + '.eml'; // Use message ID suffix in the file name
var emailFile = createUniqueFile(tempFolder, fileName, eml);
}
// Retrieve all files in the folder if it exists
var emailButtons = [];
if (tempFolder) {
var files = tempFolder.getFiles();
// Create buttons for each email file in the folder
while (files.hasNext()) {
var file = files.next();
if (!file.isTrashed() && file.getName().indexOf('.zip') === -1) { // Check if file is not .zip
var emailId = file.getId();
var emailSubject = file.getName().replace(/\.eml$/, ''); // Remove extension
var button = CardService.newTextButton()
.setText(emailSubject)
.setOnClickAction(CardService.newAction()
.setFunctionName('handleEmailSelection')
.setParameters({ 'emailId': emailId }));
emailButtons.push(button);
}
}
}
// Add a button to remove all emails from the folder
var clearAllButton = CardService.newTextButton()
.setText('Clear All')
.setOnClickAction(CardService.newAction().setFunctionName('clearAllEmails'));
var buttonSet = CardService.newButtonSet();
for (var i = 0; i < emailButtons.length; i++) {
buttonSet.addButton(emailButtons[i]);
}
buttonSet.addButton(clearAllButton);
// Check if there are files to create a zip file
var hasFiles = emailButtons.length > 0;
// Add a button to produce the zip file when there are files
if (hasFiles) {
var zipButton = CardService.newTextButton()
.setText('Create Zip File')
.setOnClickAction(CardService.newAction().setFunctionName('createZipFile'));
buttonSet.addButton(zipButton);
}
// Create a section with the button set
var section = CardService.newCardSection().addWidget(buttonSet);
// Create a card with the section
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle('Selected Emails'))
.addSection(section)
.build();
return [card];
}
// Function to create a file with a unique name in the given folder
function createUniqueFile(folder, fileName, content) {
var existingFiles = folder.getFilesByName(fileName);
if (existingFiles.hasNext()) {
// If a file with the same name already exists, return that file
return existingFiles.next();
} else {
// Otherwise, create a new file with the unique name
return folder.createFile(fileName, content);
}
}
// Function to handle email selection
function handleEmailSelection(e) {
// Retrieve the selected email ID from the event parameters
var emailId = e.parameters.emailId;
// Get the file by ID and delete it
var file = DriveApp.getFileById(emailId);
file.setTrashed(true);
// Reload the add-on card after handling email selection
return loadAddOn();
}
// Function to remove all emails from the folder
function clearAllEmails() {
var tempFolder = DriveApp.getFoldersByName('temp').next();
var files = tempFolder.getFiles();
while (files.hasNext()) {
var file = files.next();
file.setTrashed(true);
}
// Reload the add-on card after clearing all emails
return loadAddOn();
}
// Function to create a zip file
function createZipFile() {
var tempFolder = DriveApp.getFoldersByName('temp').next();
var files = tempFolder.getFiles();
var blobs = [];
while (files.hasNext()) {
blobs.push(files.next().getBlob());
}
var zipBlob = Utilities.zip(blobs, 'selected_emails.zip');
var zipFile = tempFolder.createFile(zipBlob);
return CardService.newActionResponseBuilder()
.setNotification(CardService.newNotification()
.setType(CardService.NotificationType.INFO)
.setText('Zip file created.'))
.setOpenLink(CardService.newOpenLink().setUrl(url))
//.addSection(CardService.newCardSection()
//.addWidget(CardService.newTextParagraph().setText("Click <a href="+ url +">here</a> to download .zip")))
.build();
}
// Entry point for the add-on
function buildAddOn(e) {
return loadAddOn(e);
}