Scripts not working for anonymous users, Google sp

2019-08-05 18:29发布

问题:

I am currently editing a Google spreadsheet that is set up for public editing by logged-in and anonymous users (I am not the owner). I have created a script within the spreadsheet and assigned it to a drawing (Insert Drawing). I have also assigned a script to an image.

There are two unfortunate oddities: 1) The drawing does not appear if you are anonymous (I use incognito mode?). You can see the box, and right-click it for the menu (and even delete it) but you don't see the drawing itself. You are also, for some reason, unable to add new drawing as an anonymous user - you get 'You need permission' and the curious 'You are signed in as .'. The image does appear though, and you can add your own images 2) The scripts don't run for anonymous users - either the script assigned to the image (visible) or the script attached to the drawing.

The script on the image is as follows:

function messageBarnImage() {
  Browser.msgBox("there is a message here")
}

The script runs fine when I am logged in.

I wonder whether the issue is related to this answer: https://stackoverflow.com/a/18412704/1427742 but I don't have enough reputation to comment on it (I don't understand what it means) and I'm not sure it is the same.

Question: how do I get my scripts to button-scripts to display and run?

回答1:

The post you refer to has a clear answer : any call to spreadsheet service will require authorization to work. This implicitly excludes anonymous users. See Zig Mandel answer for explanation on how to solve that (reproduced below)

The button will run under the users permission. If its anonymous it wont run if it needs any api permission (like reading the spreadsheet) since there is no user. For non anonymous users, you need to add a menu item that will trigger the google permission dialog (just do any dumb api call from it). After the user approves it will be able to click the image button