Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I work on a Google Drive with other people and we have multiple google sheets. I defined a "OnEdit" function to get the name of the people editing a specific column in a sheet, so that I can send to discord the following message "User X has updated the sheet Y."

But I can't get the name of the current user (except me). I use the following script:

function getCurrentUserEmail() {
        var protection = SpreadsheetApp.getActive().getRange('A1').protect();
       
        protection.removeEditors(protection.getEditors());
        var editors = protection.getEditors();
        if (editors.length === 2) {
            var owner = SpreadsheetApp.getActive().getOwner();
            editors.splice(editors.indexOf(owner), 1);
        }
        var userEmail = editors[0];
        protection.remove();
return userEmail;}

I believe the user editing the script doesn't have the authority to execute "removeEditors"

The classic "Session.getActiveUser()" also doesn't work for anyone but me (it returns a blank)

It is starting to become frustrating; if it is a problem of access rights, what can I do?

question from:https://stackoverflow.com/questions/65902071/google-scripts-how-to-get-the-name-email-of-the-active-editor

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
219 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your users would need to authorise the script for it to work - this is so that you can't collect user data without their permission.

You could maybe try to run an onOpen check to see if the user has already authorised your script, and if not, prompt them to execute a placeholder function.

You could log authorisation from users (eg. on a hidden sheet).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...