Hello, Volar.
Great question! When utilizing a shared Google Apps Script library with several Gmail accounts, each user will run the script in their own account context. This implies that Gmail search queries, labels, and inbox content will be particular to the authorized user executing the trigger, eliminating the need to hard-code numerous accounts.
You may allow each user to define their own inputs (such as search phrases) via:
Option 1: Use PropertiesService.getUserProperties():
This stores per-user search inputs securely and privately. Library Function (Reusable by all users):
const cleanInboxFromProperties = () => {
const userProps = PropertiesService.getUserProperties();
const queries = JSON.parse(userProps.getProperty("GMAIL_CLEANUP_QUERIES") || "[]");
queries.forEach(query => {
const threads = GmailApp.search(query);
threads.forEach(thread => {
thread.moveToTrash(); // or thread.moveToArchive()
});
});
}
//One-time Setup Function for Each User
const setUserQueries = () => {
const searchQueries = [
'label:promotions',
'subject:"deal of the day"',
];
PropertiesService.getUserProperties().setProperty("GMAIL_CLEANUP_QUERIES", JSON.stringify(searchQueries));
}
Option 2: Store User Config in a Shared Google Sheet:
- Each user is assigned their own row and may specify their search strings there.
- The library accesses the active user's email using Session. getActiveUser(). getEmail().
- It corresponds with the row on the sheet.
- Uses the appropriate search strings.
Periodic Trigger Notes:- As long as each user configures their own trigger in their own account, the script runs separately in their Gmail environment; there's no need to manage cross-account data unless you're using a service account, which Apps Script does not support for Gmail APIs.
- Use UserProperties to keep data secure and specific to users.
- There is no overlap or exposure between users.
- Simple to update inputs by account.
- Cleaner, more modular code
Let me know if you'd like a sample project with both options wired up!