I want to change the sharing 'visibility' of currently stored documents from 'anyone with the link may view' to 'private'. This is distinct from removing named viewers and editors.
3 Answers
Unfortunately, the GAS has a very limited support of the documents visibibility. There is no functionality to change this option for the DocsList.File and DocumentApp.Document classes. The Spreadsheet
class has the setAnonymousAccess method using which is possible to set if a spreadsheet is public.
Please open a new feature request on the issue tracker if this feature is important for you.

- 8,482
- 4
- 30
- 44
There is an easy way to get what you want using a method that has already been mentioned in this post
You can set the sharing / visibility parameters of any document by moving it to a shared folder. If you remove it from the shared folder then it is no long shared and that is what you wanted to do didn't you ?
So all you need to do is not to use individual sharing parameters on files but rather use the folder structure to share your files.
As a reminder, the code could be something like this to add to the folder :
function sharebyFolder(){
var file = DocsList.getFileById('docId');
var folder = DocsList.getFolderById('shared folder Id');
file.addToFolder(folder)
}
and to remove it :
function UnsharebyFolder(){
var file = DocsList.getFileById('docId');
var folder = DocsList.getFolderById('shared folder Id');
file.removeFromFolder(folder)
}

- 1
- 1

- 45,904
- 7
- 105
- 131
-
Yes I thought that was the answer too, but it is not the answer to the problem I have. Given a document with shared set to anyone with link at domain can view'. Move that document into a folder which is set to 'private, only people listed below can access'. Look at the sharing on the moved document and you will see it is unchanged. If the document has the access 'anyone can view' then moving it to a private folder will change its sharing but only to 'anyone at domain can view' NOT to private. – DavidF Oct 16 '12 at 10:09
-
This depends on how you defined the sharing properties in the first place. As I said, it has to be defined using folders and not using own document properties. I'd suggest you set the doc to 'private' first, then add to shared folder -test it- and then remove it -test again- and see the result. (test access from another account) – Serge insas Oct 16 '12 at 10:21
-
No argument about what I SHOULD have done. This is the result of an oversight at migration time for me, but I guess a reasonable thing to occur in normal operation. – DavidF Oct 16 '12 at 18:11
-
To clarify (I hope): an upload to a folder will take on the access/sharing properties of the folders IF the default setting of the domain is for documents to be "Private". However it will get the default setting where that setting is "Any in domain can view". This is not a problem for a few docs but a major upload gives you the problem I was asking about ... will be reloading! – DavidF Oct 18 '12 at 09:31
The old docs API offers a good solution: https://developers.google.com/google-apps/documents-list/#removing_sharing_permissions

- 569
- 3
- 10
-
and the new DRIVE SDK too https://developers.google.com/drive/manage-sharing ... I had hoped to avoid getting to grips with a new lot of coding while in a hurry – DavidF Oct 16 '12 at 18:16