Search code examples
google-apps-scriptgoogle-tasks-apigoogle-tasks

How to refer to my tasklist in a standalone script that will be used by other user


I have a spreadsheet linked to a standalone script that imports task from my tasklist. It works fine when I am the user but does not work in share user account.

The codes in the standalone script is

function getTasks() {

  var tasklistID="mytasklistid";  
  var tasks=Tasks.Tasks.list(tasklistID);
  return tasks

}

Code in Bound script is


function getTask(){

var tasks = TaskManagerScript.getTasks()
Browser.msgBox(tasks)

}

When I run the code in my account I get the tasks from tasklist with the specified id as expected. But when I run it from a different user account I get the the tasklist of the other user.

How do I make the code return the tasks from the list with the specified id when other users run it.


Solution

    • You are using a container-bound script of Spreadsheet, and the bound script installs a library.
    • The Spreadsheet is shared by users. You are an owner of Spreadsheet.
    • You want to make users use your task ID.
    • You want to run the script by the OnEdit event trigger.

    If my understanding is correct, how about this answer?

    OnEdit event trigger is run as owner of Spreadsheet. So when users edit the Spreadsheet, the script is run with your task list ID. But from your question, it's when I run it from a different user account I get the the tasklist of the other user.. I could confirm that in my environment, when users edit to the shared Spreadsheet, the script is run as owner (me), and my task list ID could be used. Unfortunately, I couldn't replicate your situation. So, in order to confirm this situation, can you test the following flow?

    Sample flow for testing:

    1. Create new Spreadsheet.
    2. Open the script editor and install the library of TaskManagerScript.
    3. Enable Tasks API at Advanced Google Services.
    4. Put the script to the script editor. The script is as follows.

      function getTask(e) {
        var tasks = TaskManagerScript.getTasks();
        e.source.appendRow([JSON.stringify(tasks)]);
      }
      
    5. Install OnEdit trigger to getTasks().

    6. In order to authorize, please run getTasks() by the script editor and authorize the scopes.
      • In this case, an error occurs. But don't worry. This action is used only for authorizing.
      • This authorization might not be required. But this is just in case.
    7. Share the Spreadsheet to user who is not your account.
    8. The user edits the sheet.

    By above flow, when the user edits the sheet, the result retrieved with your task list ID is returned.

    Note:

    • When OnEdit event trigger runs the script by editing the cells by users, the script is run as owner of Spreadsheet. But when the script is run by clicking a button on the sheet and the custom menu, the script is run as each user. Please be careful this.

    Reference: