Google Apps Script: Sharing your documents with ‘Editors’

Walden Systems Geeks Corner News Google Apps Script: Sharing your documents with ‘Editors’ Rutherford NJ New Jersey NYC New York City North Bergen County

When working with google docs the best results come with collaborating with other people. These groups of people can be your family members that you are sharing with or like me with your work teammates. No one lives or works in a vacuum, and the sharing feature to allow others to view or help edit your work is one of the leading features that google docs provides.

Whether your group of editors is small or large, it is a lot faster to utilize google apps script to help you manage your editor list. As your work complexity and your user list changes it is easy to add and remove editors from your google documents. In this blog we will start with adding and removing Editors. If you are looking for how to manage your Viewers visit the following URL: Sharing your google documents with ‘Viewers’


We will begin with a simple task of allowing others access to your document by adding an editor by calling a function ‘addEditors()’. And we will follow up with an opposite functionality to remove a single editor with ‘removeEditor()’. When we add an editor to a spreadsheet and the user was already on the list of editors, the method ‘addEditors’ has no effect. Another issue to keep in mind is that we can add multiple Editors with a single function call and passing an array of emails. But to remove an editor you can only do one email at a time.

Let's start with a simple function that will create a list of emails. Make sure you add valid emails otherwise google script will generate an error if the email address is invalid. You can use another Spreadsheet as your data source to keep your list of emails and populate an array with this information.


function crtEditorList() {
  var emailList = [];
  emailList.push("valid-email-01@gmail.com");
  emailList.push("valid-email-02@gmail.com");
  return(emailList);
};



Once your list is built you can call a single function to add these emails to your google document editor list.


  sSheet.addEditors(emailList);



To remove a single editor from your editors list you would call ‘removeEditor()’, function but this time only with a single email.

 sSheet.removeEditor(emailAddr);

The rest of the code is pretty straightforward and will depend on how you would like to manage your editor list access.

Putting it all together, the full code:


function delEditorFromSsheet(emailAddr, sSheet) {
  sSheet.removeEditor(emailAddr);
};

function addEditorListToSsheet(emailList, sSheet) {
  sSheet.addEditors(emailList);
};

function crtEditorList() {
  var emailList = [];
  emailList.push("valid-email-01@gmail.com");
  emailList.push("valid-email-02@gmail.com");
  return(emailList);
};

function myMainShareWithEditors() {
  const sSheet = SpreadsheetApp.getActiveSpreadsheet();
  var emailList = crtEditorList();
  addEditorListToSsheet(emailList, sSheet);
  Logger.log(sSheet.getEditors());
  delEditorFromSsheet("valid-email-01@gmail.com", sSheet);
  delEditorFromSsheet("valid-email-02@gmail.com", sSheet);
  Logger.log(sSheet.getEditors());
};