我在 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();
};