2

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()?.

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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