Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

Google Scripts: How to get the name/email of the active editor?

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

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

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...