0

I wrote a script to log who and when opens a google doc. It works fine but the doc is a template for users to fill in so want to ensure whoever needs it will make a copy of the template through apps script.

I know users need edit access for the script to work but in terms of workarounds:

  1. Is there a way for the script to still work if I give them a copy link or a template link? Does it make more sense to use the google sheet as the base and pull from the google doc ID?

If that doesn't work than: 2) Is there a way to prompt them with a menu window to copy the file but delay it by x seconds on open?

//setups a count for the file
function setup() {
  var propertyService = PropertiesService.getDocumentProperties();
  propertyService.setProperty('viewCount', '0');
}

//logs the email and date of the user accessing the file
function onOpen(e) {
 var count = parseInt(PropertiesService.getDocumentProperties().getProperty('viewCount'))+1;
 PropertiesService.getDocumentProperties().setProperty('viewCount', count);
 Logger.log(count);

 var sheet = SpreadsheetApp.openById([spreadsheet ID]);
 var user = Session.getActiveUser().getEmail();
 var date = new Date();

 sheet.getSheetByName('View Count').appendRow([user,date]);
}

1 Answers1

1

You can add a menu item that creates a copy of the template for the user, logs the copy to the same sheet where you track who opens the file, and prompts the user to open their copy of the file to start working.

Steps

First: add these 3 functions.

  1. The first, copyFile, creates a copy of the active document and names it the same plus the user's email. Then it logs the activity to the same change log, adding the new file's ID to the row. Finally, thanks to this nifty function it pops up a window with a link to the new file so the user can easily open it and start working.
  2. The second, showAnchor, is the function that generates the HTML dialog box which presents the user a link to their new file.
  3. The third, createMenu, adds a new menu item to the Google Docs nav bar, which prompts the user to copy the document. This, of course, triggers the copyFile function.
function copyFile() {
  var thisDoc = DocumentApp.getActiveDocument()
  var sheet = SpreadsheetApp.openById([spreadsheet ID]);
  var user = Session.getActiveUser().getEmail(); 
  var date = new Date();
  
  var newCopy =  DriveApp.getFileById(thisDoc.getId()).makeCopy(thisDoc.getName() + " " + user)

  sheet.getSheetByName('View Count').appendRow([user,date,newCopy.getId()]);

  var url = newCopy.getUrl()
  showAnchor('Open Your File',url);
}

function showAnchor(name,url) {
  var html = '<html><body><a href="'+url+'" target="blank" onclick="google.script.host.close()">'+name+'</a></body></html>';
  var ui = HtmlService.createHtmlOutput(html)
  DocumentApp.getUi().showModelessDialog(ui," ");
}



function createMenu() {
  const ui = DocumentApp.getUi();
  const menu = ui.createMenu("Copy This Template");
  menu.addItem("Copy", "copyFile");
  menu.addToUi();
}

Then: add a call to createMenu() at the end of your onOpen script:

function onOpen(e) {
 var count = parseInt(PropertiesService.getDocumentProperties().getProperty('viewCount'))+1;
 PropertiesService.getDocumentProperties().setProperty('viewCount', count);
 Logger.log(count);

 var sheet = SpreadsheetApp.openById([spreadsheet ID]);
 var user = Session.getActiveUser().getEmail();
 var date = new Date();

 sheet.getSheetByName('View Count').appendRow([user,date]);

 createMenu();
}
Marcelo Guerra
  • 123
  • 1
  • 8