3

I've used this script to create a Google text document (like word document) with Google Spreadsheet. But, when I do so, the new text document created from a template is placed in the default choosen folder. And, after that, I have to go to that folder, find the new document and open it. The million dollars question is: how could I make the knew created text document automatically open? (I think that if it wasn't a Google text Document but a spreadsheet I should use 'SpreadsheetApp.getActive()').

Thanks for any help and no, there are no million bucks.

craftApprentice
  • 2,697
  • 17
  • 58
  • 86

1 Answers1

2

There is no way to automatically open a document, neither a spreadsheet without having the user do something to open it (at least click a link).

What you could do is to show a popup in your spreadsheet with a link that would open that document.

This is quite simple to do from a spreadsheet script, please refer to this post and to the docslist documentation.(use doc ID to access docsList.getUrl()


EDIT : following your comment, here is a possible way to automatically hide the popup, I included this in a full demo code.

function test(){
  var id = "1cZCL7T-enU0yJZnCb0WM0NeqXDHjnnBUyvs98vsyzwU";// test document (shared in view only)
  var Doc = DocsList.getFileById(id);
  showURL('Open document named "'+Doc.getName()+'"',Doc.getUrl());
}

function showURL(nameToShow,href){
  var app = UiApp.createApplication().setHeight(50).setWidth(300);
  app.setTitle("Show URL");
  var link = app.createAnchor(nameToShow, href);
  app.add(link);  
  var handler = app.createServerHandler('hide');
  link.addClickHandler(handler);// add serverHandler to the link itself
  var doc = SpreadsheetApp.getActive();
  doc.show(app);
}

function hide(){
  var app = UiApp.getActiveApplication().close();// close the popup window
  return app;// apply change
}
Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Thanks, Serge. Could you tell me how to automatically close de UI after the link is clicked? Thank you again! – craftApprentice Jul 27 '13 at 23:40
  • Thank you for the showURL function! It's working for me with a slight modification: I had to comment out the line of code that adds the hide handler to the link. When I do that, it works fine (but the popup window doesn't automatically close). When I tried the original code, when I click the link to open the doc, I get the 'Aw snap' Google error message. I'm using the new version of Sheets, so maybe there's something with this version that makes the handler to hide the popup interfere with the displaying of the doc? – Carrie May 19 '14 at 00:35
  • UiApp is been deprecated, as such the answer is no longer working. – Jacopo Tedeschi Dec 02 '22 at 17:21