Getting all viewers of spreadsheet using App Script

后端 未结 1 1186
别跟我提以往
别跟我提以往 2021-01-24 02:13

i wrote code for getting all viewers of spreadsheet in app script . i have used getViewers() method to get viewers names who actually viewed it. but that method is returning me

相关标签:
1条回答
  • 2021-01-24 02:40

    Answer:

    It is not possible to get a list of people that have opened your a Google Drive file using Google Apps Script - a method that returns this list does not exist. The getViewers() method returns the list of people with view and comment permissions for a file, while getEditors() retrieves the list of people that have edit permissions.

    The Issue:

    is there any other way that i can get all viewers of spreadsheet.? is there any web automation tools that can solve my problem?

    There is no way of getting viewers of a Google Sheet as this is a huge security issue. This information is not stored and is therefore not retrievable.

    Workaround:

    You can make a custom function which stores the username of a person when they open the file - though be aware that triggers have restrictions and will only run if the person opening the file has edit access. I have included a list of Apps Script Trigger Restrictions below for you to look through and decide what is the best approach for you.

    Code:

    function onOpen(e) {
      var user = e.user.getEmail();
      // do some code to store or save this parameter
      // for example save it to a hidden sheet or email it to yourself
      // though an email would require an installable trigger to be made
    }
    

    Simple Trigger Restrictions:

    These are not all of the restrictions (full restrictions are available here), but these are the ones that I believe to be most relevant for you.

    As per the Google Apps Script Simple Triggers documentation:

    • They do not run if a file is opened in read-only (view or comment) mode.
    • They cannot access services that require authorization. For example, a simple trigger cannot send an email because the Gmail service requires authorization, but a simple trigger can translate a phrase with the Language service, which is anonymous.
    • They can modify the file they are bound to, but cannot access other files because that would require authorization.
    • They may or may not be able to determine the identity of the current user, depending on a complex set of security restrictions.

    This last point is important - getting information about the current user is possible depending on the security policies of the the G Suite domain. A detailed explanation of this can be found in the getActiveUser() method documentation:

    If security policies do not allow access to the user's identity, User.getEmail() returns a blank string. The circumstances in which the email address is available vary: for example, the user's email address is not available in any context that allows a script to run without that user's authorization, like a simple onOpen(e) or onEdit(e) trigger, a custom function in Google Sheets, or a web app deployed to "execute as me" (that is, authorized by the developer instead of the user). However, these restrictions generally do not apply if the developer runs the script themselves or belongs to the same G Suite domain as the user.

    There are big security issues with getting a list of people that have viewed a file, for good reason, and so what you are looking to do it highly restricted by Google. I hope that the information I have provided will be helpful to you.

    References:

    • Class File of Google Apps Script
      • getViewers() method of Class File
      • getEditors() method of Class File
    • Simple Triggers
      • onOpen(e) Trigger
      • Simple Trigger Restrictions
    • Installable Triggers
    • Event objects
    • Class User of Apps Script
      • User.getEmail() method
    • Class Session
      • getActiveUser() method of Session Class
    0 讨论(0)
提交回复
热议问题