Take the 2-minute tour ×
Web Applications Stack Exchange is a question and answer site for power users of web applications. It's 100% free, no registration required.

I've used this script to create a Google 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 chosen folder. And, after that, I have to go to that folder, find the new document and open it.

The million dollar question is: how could I make the newly created text document to automatically open? I think that if it wasn't a Google text Document but a spreadsheet I should use SpreadsheetApp.getActive(). Is there a document equivalent of .getActive()?.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I've found no direct way of opening a created document through Google Apps Script (GAS), via GAS. There's however an indirect way, which is presented below. It uses an Anchor element to show a pre-formatted hyperlink.

Code

// global
var ss = SpreadsheetApp.getActive();

function onOpen() {
  var menu = [{name: "Create Document", functionName: "createDoc"}];
  ss.addMenu("Extra", menu);
}

function createDoc() {
  var sh = ss.getActiveSheet(), aRow = sh.getActiveCell().getRowIndex();
  var title = sh.getRange(aRow, 1).getValue(), docDate = new Date(); 
  var docTitle = title + "-" + docDate, doc = DocumentApp.create(docTitle);
  var url = doc.getUrl();

  // create app and panel
  var app = UiApp.createApplication().setTitle("Open Google Document")
    .setHeight(50).setWidth(400);
  var vPanel = app.createVerticalPanel()
    .add(app.createAnchor(docTitle, url));

  // add to app
  app.add(vPanel);
  ss.show(app);  
}

Screenshot

enter image description here

Remarks

This piece of code creates a new menu entry called "Extra" upon opening of the file. The selection "Create Document" will create (in the back gound) a Google Document and displays a panel with a hyperlink to open the document.

Example

I've created an example file for you: How To Open Document

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.