- Your Spreadsheet is shared with users.
- You want to make users run your script by clicking a button on Spreadsheet.
- You want to make users use your task list.
If my understanding is correct, how about this answer? Please think of this as just one of several answers.
Reason of your issue:
When users run your script on Spreadsheet by clicking a button on the sheet, the script is run as each user. By this, users cannot find mytasklistid
of var tasklistID="mytasklistid"
from user's task list. I think that the reason of your issue is this.
Workaround:
As one of several workarounds, how about using Web Apps? This is also mentioned by TheMaster. When Web Apps is deployed as Execute the app as: Me
, even if other users run the script of Web Apps, the script is run as owner of Web Apps (you). Under this situation, when users run the script by clicking a button, users request to Web Apps you deployed using UrlFetchApp. By this, users can use your task list ID. This workaround uses this situation. The flow of this workaround is as follows.
- Deploy the script as Web Apps.
- Set
Execute the app as:
as Me
.
- Set
Who has access to the app:
as Anyone, even anonymous
. This setting is for testing.
- Users use the deployed Web Apps like the external API.
By this, users can use your task list.
Sample script:
The sample script is as follows. There are 2 sample scripts for Server side (your script) and Client side (user's script), respectively. You can also put both scripts in the same project.
- Please copy and paste "Server side" to your project and deploy it as Web Apps.
- About how to deploy Web Apps, you can see at here.
- Please copy and paste "Client side" to user's project (the bound script of Spreadsheet including the button). At this time, please set the URL of Web Apps like
https://script.google.com/macros/s/###/exec
.
Server side: your script
function doGet() {
var tasks = getTasks();
return ContentService.createTextOutput(JSON.stringify(tasks)).setMimeType(ContentService.MimeType.JSON);
}
function getTasks() {
var tasklistID="mytasklistid";
var tasks=Tasks.Tasks.list(tasklistID);
return tasks;
}
Client side: user's script
Please assign this function to the button.
function getTask(){
var url = "https://script.google.com/macros/s/###/exec";
var tasks = UrlFetchApp.fetch(url);
Logger.log(tasks)
}
Note:
- When you modify your script of Web Apps, please redeploy as new version. By this, the latest script is reflected to Web Apps. This is important point for using Web Apps.
- In this workaround, the library and client are separated. Users as the client retrieve the data from Web Apps.
References:
If I misunderstood your question and this was not the result you want, I apologize.