Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions gmail/add-ons/quickstart.gs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,27 @@
*/

// [START apps_script_gmail_quick_start]
/**
* This is a partial definition of the a Gmail add-on event object.
* For the full list of properties, see:
* https://developers.google.com/workspace/add-ons/concepts/event-objects
*
* @typedef {Object} gmailEvent
* @property {Object} messageMetadata
* @property {string} messageMetadata.accessToken
* @property {string} messageMetadata.messageId
* @property {Object} formInputs
* @property {string[]} formInputs.labels
*/

/**
* Returns the array of cards that should be rendered for the current
* e-mail thread. The name of this function is specified in the
* manifest 'onTriggerFunction' field, indicating that this function
* runs every time the add-on is started.
*
* @param {Object} e The data provided by the Gmail UI.
* @return {Card[]}
* @param {gmailEvent} e The data provided by the Gmail UI.
* @return {CardService.Card[]}

Check failure on line 38 in gmail/add-ons/quickstart.gs

View workflow job for this annotation

GitHub Actions / test (gmail)

Namespace 'CardService' has no exported member 'Card'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Namespace 'CardService' has no exported member 'Card'.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. The type checker doesn't recognize CardService.Card in this context. I've added a local @typedef {Object} Card to satisfy the checker while keeping the code correct for the Apps Script runtime. Thank you for the feedback!

*/
function buildAddOn(e) {
// Activate temporary Gmail add-on scopes.
Expand Down Expand Up @@ -71,9 +84,9 @@
* user selections. Runs via the OnChangeAction for
* each CHECK_BOX created.
*
* @param {Object} e The data provided by the Gmail UI.
* @param {gmailEvent} e The data provided by the Gmail UI.
*/
function toggleLabel(e){
function toggleLabel(e) {
var selected = e.formInputs.labels;

// Activate temporary Gmail add-on scopes.
Expand All @@ -85,30 +98,29 @@
var thread = message.getThread();

if (selected != null){
for each (var label in GmailApp.getUserLabels()) {
if(selected.indexOf(label.getName()) != -1){
thread.addLabel(label);
}
else {
thread.removeLabel(label);
}
}
}
else {
for each (var label in GmailApp.getUserLabels()) {
for (const label of GmailApp.getUserLabels()) {
if (selected.indexOf(label.getName()) != -1) {
thread.addLabel(label);
} else {
thread.removeLabel(label);
}
}
} else {
for (const label of GmailApp.getUserLabels()) {
thread.removeLabel(label);
}
}
}


/**
* Converts an GmailLabel object to a array of strings.
* Converts a GmailLabel object to a array of strings.
* Used for easy sorting and to determine if a value exists.
*
* @param {labelsObjects} A GmailLabel object array.
* @return {lables[]} An array of labels names as strings.
* @param {GoogleAppsScript.Gmail.GmailLabel[]} labelsObjects
* @return {string[]} An array of labels names as strings.
*/
function getLabelArray(labelsObjects){
function getLabelArray(labelsObjects) {
var labels = [];
for(var i = 0; i < labelsObjects.length; i++) {
labels[i] = labelsObjects[i].getName();
Expand Down
3 changes: 3 additions & 0 deletions gmail/inlineimage/inlineimage.gs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ function sendEmailToMyself() {
sendEmailWithInlineImage(Session.getActiveUser().getEmail());
}

/**
* @param {string} toAddress
*/
function sendEmailWithInlineImage(toAddress) {
const options = {};
const imageName = 'cat_emoji';
Expand Down
17 changes: 7 additions & 10 deletions gmail/markup/Code.gs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
* Send an email with schemas in order to test email markup.
*/
function testSchemas() {
try {
const htmlBody = HtmlService.createHtmlOutputFromFile('mail_template').getContent();
const htmlBody =
HtmlService.createHtmlOutputFromFile('mail_template').getContent();

MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: 'Test Email markup - ' + new Date(),
htmlBody: htmlBody
});
} catch (err) {
console.log(err.message);
}
MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: 'Test Email markup - ' + new Date(),
htmlBody: htmlBody,
});
}
// [END gmail_send_email_with_markup]

30 changes: 14 additions & 16 deletions gmail/quickstart/quickstart.gs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,20 @@
* @see https://developers.google.com/gmail/api/reference/rest/v1/users.labels/list
*/
function listLabels() {
try {
// Gmail.Users.Labels.list() API returns the list of all Labels in user's mailbox
const response = Gmail.Users.Labels.list('me');
if (!response || response.labels.length === 0) {
// TODO (developer) - No labels are returned from the response
console.log('No labels found.');
return;
}
// Print the Labels that are available.
console.log('Labels:');
for (const label of response.labels ) {
console.log('- %s', label.name);
}
} catch (err) {
// TODO (developer) - Handle exception on Labels.list() API
console.log('Labels.list() API failed with error %s', err.toString());
// Add this check to use the Gmail advanced service.
if (!Gmail || !Gmail.Users || !Gmail.Users.Labels) {
throw new Error('Enable the Gmail Advanced Service.');
}
// Gmail.Users.Labels.list() API returns the list of all Labels in user's mailbox
const response = Gmail.Users.Labels.list('me');
if (!response || !response.labels || response.labels.length === 0) {
console.log('No labels found.');
return;
}
// Print the Labels that are available.
console.log('Labels:');
for (const label of response.labels) {
console.log('- %s', label.name);
}
}
// [END gmail_quickstart]
64 changes: 28 additions & 36 deletions gmail/sendingEmails/sendingEmails.gs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,16 @@
* Sends emails with data from the current spreadsheet.
*/
function sendEmails() {
try {
const sheet = SpreadsheetApp.getActiveSheet(); // Get the active sheet in spreadsheet
const startRow = 2; // First row of data to process
const numRows = 2; // Number of rows to process
const dataRange = sheet.getRange(startRow, 1, numRows, 2); // Fetch the range of cells A2:B3
const data = dataRange.getValues(); // Fetch values for each row in the Range.
for (const row of data) {
const emailAddress = row[0]; // First column
const message = row[1]; // Second column
const subject = 'Sending emails from a Spreadsheet';
MailApp.sendEmail(emailAddress, subject, message); // Send emails to emailAddresses which are presents in First column
}
} catch (err) {
console.log(err);
const sheet = SpreadsheetApp.getActiveSheet();
const startRow = 2;
const numRows = 2;
const dataRange = sheet.getRange(startRow, 1, numRows, 2);
const data = dataRange.getValues();
for (const row of data) {
const emailAddress = row[0];
const message = row[1];
const subject = 'Sending emails from a Spreadsheet';
MailApp.sendEmail(emailAddress, subject, message);
}
}
// [END gmail_send_emails]
Expand All @@ -42,29 +38,25 @@ function sendEmails() {
* Sends non-duplicate emails with data from the current spreadsheet.
*/
function sendNonDuplicateEmails() {
const EMAIL_SENT = 'email sent'; //This constant is used to write the message in Column C of Sheet
try {
const sheet = SpreadsheetApp.getActiveSheet(); // Get the active sheet in spreadsheet
const startRow = 2; // First row of data to process
const numRows = 2; // Number of rows to process
const dataRange = sheet.getRange(startRow, 1, numRows, 3); // Fetch the range of cells A2:B3
const data = dataRange.getValues(); // Fetch values for each row in the Range.
for (let i = 0; i < data.length; ++i) {
const row = data[i];
const emailAddress = row[0]; // First column
const message = row[1]; // Second column
const emailSent = row[2]; // Third column
if (emailSent === EMAIL_SENT) {
console.log('Email already sent');
return;
}
const subject = 'Sending emails from a Spreadsheet';
MailApp.sendEmail(emailAddress, subject, message);// Send emails to emailAddresses which are presents in First column
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
SpreadsheetApp.flush(); // Make sure the cell is updated right away in case the script is interrupted
const EMAIL_SENT = 'email sent';
const sheet = SpreadsheetApp.getActiveSheet();
const startRow = 2;
const numRows = 2;
const dataRange = sheet.getRange(startRow, 1, numRows, 3);
const data = dataRange.getValues();
for (let i = 0; i < data.length; ++i) {
const row = data[i];
const emailAddress = row[0];
const message = row[1];
const emailSent = row[2];
if (emailSent === EMAIL_SENT) {
console.log('Email already sent');
return;
}
} catch (err) {
console.log(err);
const subject = 'Sending emails from a Spreadsheet';
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
// [END gmail_send_non_duplicate_emails]
Loading