简体   繁体   中英

How to show a pop up on screen of a specifc user in google sheets?

I am trying to show a pop-up when a specific mentioned user opens the google sheet . The main idea is to check a specific column for an email and compare it with the email of the person who opened the google sheet. If the email matches, the pop-up should show on the screen.

function onOpen() 
{
  var result = ?
  var email = Session.getEffectiveUser().getEmail();
  var ui = SpreadsheetApp.getUi();
  if(result = email) {
    ui.alert('Important Notice');
  }
}

I am here so far, I need to get the email from a specific column in the result variable. Let's say column A has the email addresses and when the email address matches the user who opened the sheet, the pop up will be shown to that person.

If the question is how to get the contents of all cells in a column:

  • You can get all the column contents as an 1-D array by first using the method getValues() and then flat() .
  • You can check either user's email is contained in this array with indexOf() .

Sample (please refer to the documentation for a better understanding othe methods used - if necessary):

function bindMeToAnInstallableOnOpenTrigger() 
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Name of the Sheet containing emails");
  var emails = sheet.getRange("A2:A").getValues().flat();
  var email = Session.getEffectiveUser().getEmail();
  var ui = SpreadsheetApp.getUi();
  if(emails.indexOf(email) != -1) {
    ui.alert('Important Notice');
  }
}
  • Note that getEffectiveUser() is a call that requires authorization and thus will not work on a simple onOpen trigger .
  • Instead, you need to rename your function and bind an installable onOpen trigger to it.

This code is working for me but the problem is unless and until the user doesn't give permission to it by running the code once, it will not work. So, every user who is added to the sheet needs to run the Appscript once and give it permission. As this person said that it needs authorization, so I understand the problem is with my code.

function onOpen() 
{
  var range = SpreadsheetApp.getActiveSheet().getRange('A1:A15');
  var value = range.getValue();
  var email = Session.getEffectiveUser().getEmail();
  var ui = SpreadsheetApp.getUi();
  if(value = email) {
    ui.alert('Important Notice');
  }
}

Edited:

I have modified the code given to me by the mentioned user according to my understanding.

function onOpen() 
{
  popup();
}

function popup() 
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Sheet1");
  var emails = sheet.getRange('I20:I70').getValues().flat();
  var email = Session.getEffectiveUser().getEmail();
  var ui = SpreadsheetApp.getUi();
  var index = emails.indexOf(email);
  if(index > -1) {
    ui.alert('This notification is to inform you that there is a task/issue that is pending');
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM