gpt4 book ai didi

google-apps-script - 有没有办法从 Google 文档应用程序脚本打开新文档?

转载 作者:行者123 更新时间:2023-12-02 03:46:33 25 4
gpt4 key购买 nike

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

最佳答案

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

这些脚本在 Google 服务器中运行。当脚本打开 Doc 或 Spreadsheet 时,它这样做是为了访问其数据,而不是在浏览器上显示它。

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

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

关于google-apps-script - 有没有办法从 Google 文档应用程序脚本打开新文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16587781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com