Apps script library sharing with unique user data

62 views
Skip to first unread message

Volar Picante

unread,
May 3, 2025, 12:40:18 PMMay 3
to Google Apps Script Community
I have a script on a periodic trigger that deletes or archives emails from my gmail inbox.  Promotional emails, etc.  I basically pass in an array of gmail search strings, and it performs the action on any emails that match those search criteria.

I'd like to extend this to all (not just one) of my own Gmail accounts, but how exactly do I specify Gmail search strings that are unique to each Gmail account?

Basically if the apps script project library is shared among several users, how does each user specify their own particular data inputs to the project library? 

Thanks!

Google Pony

unread,
May 8, 2025, 10:05:02 AMMay 8
to Google Apps Script Community
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!

Sincerely yours,
Sandeep Kumar Vollala
Consultant
LinkedIn Logo WhatsApp Logo

Volar Picante

unread,
May 9, 2025, 6:42:18 AMMay 9
to Google Apps Script Community
Thank you Sandeep for showing me how it;s done!  I went with the PropertiesService.

Google Pony

unread,
May 11, 2025, 5:05:26 PMMay 11
to Google Apps Script Community
Hi Volar,

Thank you for raising a thoughtful and very relevant question regarding extending your Gmail cleanup automation to multiple accounts using a shared Apps Script library.

You're absolutely on the right track by considering how to let each user specify their own unique input - such as Gmail search strings - without hardcoding or modifying the core script. 

Below are two proven solutions that scale cleanly:

Recommended Solution: PropertiesService.getUserProperties()
This is ideal for securely storing each user’s search queries independently, especially when the script is shared as a library.

1. Library Method (shared across all users):
const cleanInbox = () => {
  const queries = JSON.parse(PropertiesService.getUserProperties().getProperty("GMAIL_CLEANUP_QUERIES") || "[]");
  queries.forEach(q => {
    GmailApp.search(q).forEach(thread => thread.moveToTrash()); // or moveToArchive()
  });
};


2. User Setup Method (each user runs once to set their own preferences):
const setUserQueries = () => {
  const queries = [
    'label:promotions',
    'subject:"weekly sale"',
  ];
  PropertiesService.getUserProperties().setProperty("GMAIL_CLEANUP_QUERIES", JSON.stringify(queries));
};

Benefits:
  • Each user maintains their own input.
  • No risk of data leakage between users.
  • Clean and modular codebase.
Alternate Approach: Shared Google Sheet Config:
  • If you want a non-code option for users to manage their inputs:
  • Create a shared Google Sheet.
  • Assign each user one row with their email and query list.
  • Use Session.getActiveUser().getEmail() to identify the active user and load their row.
  • This option is best when you want centralized admin oversight or when users are less comfortable setting up scripts directly.

Trigger Considerations:
Each user must set up their own trigger within their account. Since Apps Script runs in the context of the logged-in user, the script will operate only within that Gmail inbox - no shared service account required or allowed for Gmail operations.

I'm glad to hear you implemented the PropertiesService option already! If you’d like, I can share a ready-to-use sample project with both methods pre-configured for easier adoption.

Let me know how else I can assist.

Warm regards,
Sandeep Kumar Vollala
Consultant
LinkedIn Logo WhatsApp Logo
Reply all
Reply to author
Forward
0 new messages