SetOwner() Not working anymore

241 views
Skip to first unread message

Charlton Nicholl

unread,
Nov 28, 2022, 10:23:55 PM11/28/22
to Google Apps Script Community
Hello, I have had this function working on my google sheets for a number of years. I recently started using google sheets again and this code no longer works.

Here is my problem, I have a shared folder which houses a google sheets document that is a template for other users to copy.
Once the user copies this code would automatically set me as the owner. Unfortunately this no longer works and I am looking for another way.

Here are two examples of this code that should/would work.

function setOwner() {
// The Code Below Will Set Document Ownership
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var s = SpreadsheetApp.getActiveSheet();
 var files = DriveApp.getFilesByName(ss.getName());
 while (files.hasNext()) {
   var file = files.next();
   var owner = file.setOwner("1...@gmail.com");
 } 
 var smain = SpreadsheetApp.setActiveSheet(ss.getSheetByName('Sheet1'));
 smain.showSheet();
 s.hideSheet();
 
}

function transfer() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = SpreadsheetApp.getActiveSheet();
  var user = Session.getActiveUser().getEmail();  // Modified
  var folder = DriveApp.getFolderById('Folder ID');
  var files = folder.getFilesByName(ss.getName());
  while (files.hasNext()) {
    var file = files.next();
    if (file.getOwner().getEmail() == user) file.setOwner('123...@gmail.com');  // Modified
  }
}

cwl...@gmail.com

unread,
Dec 1, 2022, 9:28:02 AM12/1/22
to Google Apps Script Community
If you want to retain ownership, instead of them making a copy, have them run a function or press a button (or some trigger) that runs a function where you create the copy and then share the file with them.
It won't prevent them from making further copies, but that is how I would approach it. 

Charlton Nicholl

unread,
Dec 1, 2022, 7:00:28 PM12/1/22
to Google Apps Script Community
Could you point me in the right direction or insert an example please :D

Brett Lynden Everingham

unread,
Dec 1, 2022, 7:43:15 PM12/1/22
to google-apps-sc...@googlegroups.com
Your Awesome thanks for the Detective work 

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/58e3c439-efee-45b0-98bd-82cabacbb157n%40googlegroups.com.

cwl...@gmail.com

unread,
Dec 2, 2022, 9:53:39 AM12/2/22
to Google Apps Script Community
Say the user presses a button "Make Copy". You could do something like this:

function makeCopy() {
  var email = Session.getActiveUser().getEmail(); //get the user's email who ran the script
  var ssID = SpreadsheetApp.getActiveSpreadsheet().getId(); //get the current Spreadsheet's ID
  var copyID = DriveApp.getFileById(ssID).makeCopy().getId(); //make a copy of the spreadsheet and retrieve the new ID
  DriveApp.getFileById(copyID).addEditor(email); //add the user as an editor to the new copy
}
Reply all
Reply to author
Forward
0 new messages