3

我在 Google 文档中有一个电子表格,其中包含小型企业的每日库存。我编写了一个脚本,该脚本创建脚本的副本,然后将期末股票复制到期初股票,然后将各个字段(收到的现金等)归零。

问题是似乎没有办法更改用户当前正在查看的文档,因此他们必须运行脚本,然后返回文件夹并打开新的日期文件并进行编辑。

代码看起来像..

/**
 * 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();
};
4

1 回答 1

1

Darius,您的 Google App 脚本无法导航到其他网址。您最多可以做的是在单元格中显示一个超链接,以便用户可以单击它:它将在其他选项卡中打开,当然,它需要用户操作才能这样做。(我不想提出 HTML 服务的主题,这超出了范围。)

这些脚本在 Google 服务器中运行。当脚本打开文档或电子表格时,它会访问其数据,但不会在浏览器上显示它。

只有“容器绑定脚本”(您从文档或电子表格编写的脚本)可以与用户界面进行温和的交互,例如添加菜单或面板。

如果你想显示一些数据,你可以“range.setValues”它,或者你可以用一个独立的脚本做一个网页,使用 HTML 服务,来显示从电子表格中获得的数据。

于 2014-02-24T19:13:29.143 回答