Is there a way to open a new document from a Googl

2019-02-24 23:31发布

问题:

I have a spreadsheet in Google docs which has daily stock for a small business. I have written a script which creates a copy of the script and then copies the closing stock to the opening stock and then zeroes out various fields (cash received etc).

The problem is that there doesn't seem to be a way to change the document the user is currently viewing so they have to run the script, then go back to the folder and open the new day file and edit that.

The code looks like..

/**
 * Setup the spreadsheet for a new day
 */
function newDay(date) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  /* Generate localtime version
   * Do this instead?
   * newSs.setSpreadsheetTimeZone('Australia/Adelaide'); */
  var calTimeZone = 'Australia/Adelaide';
  var timeStamp = Utilities.formatDate(date, calTimeZone, 'yyyy/M/d');

  var newName = Utilities.formatDate(date, calTimeZone, 'yyyyMMdd');

  /* Save new version and set ss to that new spreadsheet */
  var newSs = ss.copy(newName);
  Logger.log("Saved to " + newName + ", URL " + newSs.getUrl());
  var id = newSs.getId();

  /* Get file ID for the new spreadsheet */
  var file = DocsList.getFileById(id);

  /* Get handle for Java Drive folder */
  var folder = DocsList.getFolderById('xxxxxx');

  /* Remove new file from all its parents */
  Logger.log("parents - " + file.getParents());
  var parents = file.getParents();
  for (var i in parents) {
    file.removeFromFolder(parents[i]);
  }

  /* Add file into the Java Drive folder */
  file.addToFolder(folder);

  var dateRange = newSs.getRangeByName('date');
  dateRange.setValue(timeStamp);

  // Munge other ranges as needed

  Logger.log("Done");

  var app = UiApp.createApplication().setTitle('All done');
  var dialog = app.createDialogBox(false, true);
  var label = app.createLabel('The new file has been created named ' + newName + ' and is ready to edit. This file is unmodified, please close it and open the new one', true);
  dialog.add(label);
  dialog.show();
  ss.show(app);
};

/* Draw the date picker and setup hooks */
function drawUI() {
  var app = UiApp.createApplication().setTitle('Select date');
  var panel = app.createVerticalPanel();  
  var datePicker = app.createDatePicker();
  var submit = app.createButton('Submit');
  datePicker.setId('datePicker');
  var clickHandler = app.createServerHandler('handleSubmit');
  clickHandler.addCallbackElement(panel);
  submit.addClickHandler(clickHandler);
  panel.add(datePicker);
  panel.add(submit);
  app.add(panel);
  var d = Date();

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.show(app);

}

/* Run newDay in response to the user clicking submit */
function handleSubmit(e) {
  var app = UiApp.getActiveApplication();
  var date = e.parameter.datePicker;
  newDay(date);
  return app.close();
}

/**
 * Adds a custom menu to the active spreadsheet, containing a single menu item
 * for invoking the newDay() function specified above.
 * The onOpen() function, when defined, is automatically invoked whenever the
 * spreadsheet is opened.
 * For more information on using the Spreadsheet API, see
 * https://developers.google.com/apps-script/service_spreadsheet
 */
function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "New Day",
    functionName : "drawUI"
  }];
  sheet.addMenu("Scripts", entries);

  //drawUI();
};

回答1:

Darius, there is no way your Google App Script can navigate to other url. The most you can do is to present a hyperlink in a cell, so the user can click it: it will open in other tab, and of course, it needs user action to do so. (I don't want to bring the subject of HTML services, that's out of scope.)

These scripts run in Google servers. When a script opens a Doc or Spreadsheet, it do so to access its data, but not to show it on the browser.

Only "container bound scripts" (those you write from a document or spreadsheet) can mildly interact with the user interface, say adding menus or panels.

If you want to show some data, you can "range.setValues" it, or you can do a web page with a standalone script, using HTML Services, to show data obtained from spreadsheets.