How to get the file URL from file name in Google Sheets with correct Authorization via custom function/script

后端 未结 3 1899
小蘑菇
小蘑菇 2020-12-11 13:43

I would like to create a custom function that pulls a Drive URL from a file name in Google Sheets.

So, using the code below:

  1. If I have a valid file nam
3条回答
  •  甜味超标
    2020-12-11 14:29

    @I'-'I's answer is correct. Although I'm not sure whether this is what you want, how about this workaround? I have also experienced the same issue. At that time, I had used the following workaround.

    • Set and get access token using PropertiesService.

    The flow is as follows.

    Flow:

    1. Set access token every 1 hour by the time-driven trigger.
      • By this, the access token is updated every 1 hour. Because the expiration time of access token is 1 hour.
    2. When the custom function is run, it gets the access token using PropertiesService.
      • By this, the access token can be used.

    Modified script:

    Please install this function as the time-driven trigger. Of course, you can run manually this function.

    function setAccessToken() {
      PropertiesService.getScriptProperties().setProperty("accessToken", ScriptApp.getOAuthToken());
    }
    

    In your script, please modify as follows.

    From:
    var params = {
      method: "GET",
      headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
      muteHttpExceptions: true
    };
    
    To:
    var params = {
      method: "GET",
      headers: {"Authorization": "Bearer " + PropertiesService.getScriptProperties().getProperty("accessToken")},
      muteHttpExceptions: true
    };
    

    Note:

    • In this case, the owner of access token is the owner of project.
    • I think that this can be also used by CacheService.

    Reference:

    • PropertiesService

提交回复
热议问题