2

is it possible to link to a google form from a custom menu. I have a google sheet set up that receives the results of a google form. I have a custom menu, but want to link directly to the 'viewform' url of the google form when the user selects the menu item (I realize this is not perhaps conventional use of google form, but I really don't want users directly editing the sheet to input data).

Below is what I have so far...

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('Century Items')
      .addItem('Add to RedBook', 'menuItem1')
}

function menuItem1() {
  var form = UrlFetchApp.fetch(https://docs.google.com/forms/d/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/viewform);

}

Not quite sure how to finish it and I keep getting the error "Missing ) after argument list" I've looked at other possibilities such as var form = FormApp.openByUrl('url') etc..., but not quite sure how to implement them.

I'm definitely new to java-script so any help is appreciated.

EDIT: Working Script with the help of Serge:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('Century Items')
      .addItem('Add to RedBook', 'menuItem1')
      .addToUi();
}

function menuItem1() {
showURL("https://docs.google.com/forms/x/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/viewform")
}

function showURL(href){
  var app = UiApp.createApplication().setHeight(50).setWidth(200);
  app.setTitle("Add to Redbook");
  var link = app.createAnchor('Open Red Book Form ', href).setId("link");
  app.add(link);  
  var doc = SpreadsheetApp.getActive();
  doc.show(app);
  }
mongoose36
  • 799
  • 4
  • 14
  • Thanks for accepting ;-) I added a version that will be compatible with docs and forms as well since you suggested it in your edit. – Serge insas Sep 22 '14 at 20:41

1 Answers1

0

If I understood you well (which is definitely not sure) what you should use is an anchor widget in a popup window that will allow users to open the form view url in a new tab / window.

You can use a code like this (taken from a former answer) :

function showURL(){
  var app = UiApp.createApplication().setHeight(50).setWidth(200);
  app.setTitle("Show Form URL");
  var link = app.createAnchor('open ', 'live form url');
  app.add(link);  
  var doc = SpreadsheetApp.getActive();
  doc.show(app);
  }

EDIT

Following your edit and to make it compatible with docs and forms as you suggested above (but only working in new version of spreadsheet) ,here is a "modern" version of the same function (using SpreadsheetApp.getUi())

function showURL(href){
  var app = UiApp.createApplication().setHeight(50).setWidth(200);
  var link = app.createAnchor('Open Red Book Form ', href);
  app.add(link);  
  var doc = SpreadsheetApp.getUi(); // Or DocumentApp or FormApp.
  doc.showModelessDialog(app,"Add to Redbook");
  }
Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Serge, thanks for taking the time to answer my question ... I had come across the post you indicated, but was unclear on its use. Upon further study I've got it working. One question however I take it that automatically opening the url when one clicks the menu item is not allowed instead clicking the menu item needs to launch the function that creates the popup? – mongoose36 Sep 22 '14 at 19:24
  • ok, good to know, Yes indeed, it must be a "two steps operation"...could you accept this one though ? thanks – Serge insas Sep 22 '14 at 19:25
  • This works for me. I've just started learning google scripts (trying to transition from VBA)...its a whole new world! Thanks again! – mongoose36 Sep 22 '14 at 19:34
  • *trying to transition from VBA* ... Good idea , good luck and thanks ;-) – Serge insas Sep 22 '14 at 19:41