Google Apps:复制模板表单并删除旧表单 [英] Google Apps: Duplicate template sheet and delete old sheet
问题描述
问候语:stackoverflow社区,
Greetings stackoverflow community,
我目前正在尝试使用Google Spreadsheets为我们的团队每周制作日常任务。到本周结束时,团队成员应该重置仪表板表,但我遇到了以下脚本的问题。脚本应该复制TEMPLATE表单,删除旧的仪表板,并将新的副本重命名为Dashboard:
$ b
I am currently trying to build a dashboard using Google Spreadsheets for my team for their daily tasks on a weekly basis. By the end of the week the team members should "reset" the dashboard sheet, but I am having trouble with the following script. The script should duplicate the "TEMPLATE" sheet, delete the old dashboard and rename the new copy to "Dashboard":
function resetDashboard() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('TEMPLATE');
sheet.copyTo(ss).setName('New Dashboard');
ss.setActiveSheet(ss.getSheetByName('Dashboard'));
ss.deleteActiveSheet();
ss.setActiveSheet(ss.getSheetByName('New Dashboard'));
ss.renameActiveSheet('Dashboard');
}
注意:仪表板工作表是电子表格中唯一可见的工作表。
Note: The "Dashboard" sheet is the only visible sheet in the spreadsheet.
到目前为止,这个脚本工作正常。当我重新加载电子表格时,它仍然有效。
So far this script is working fine. When I reload the spreadsheet it is still working.
我的问题是,当我关闭电子表格并从GoogleDocs概览中再次打开时,脚本仅复制模板工作表,但该函数的其余部分不再执行。
My problem is, that when I close the spreadsheet and open it again from the GoogleDocs overview the script is ONLY duplicating the "Template" sheet, but the rest of the function is not executed anymore.
这是一个错误吗?是否有一个清洁版本/脚本,我可以使用?
Is this a bug? Is there a "cleaner" version / script that I could use?
最好的问候,
Rüdiger
Best regards, Rüdiger
推荐答案
我认为问题在于,暂时你有两张同名的表。重命名前的 SpreadsheetApp.flush()
可能适用于该作业。试试这个:
$ b
I think the issue is that "momentarily" you're having two sheets with the same name. A SpreadsheetApp.flush()
before renaming might to the job. Try this:
function resetDashboard() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var newSheet = ss.getSheetByName('TEMPLATE').copyTo(ss);
ss.setActiveSheet(ss.getSheetByName('Master'));
ss.deleteActiveSheet();
SpreadsheetApp.flush(); //this guarantees that the old sheet is deleted before we proceed
newSheet.setName('Master');
ss.setActiveSheet(newSheet);
}
这篇关于Google Apps:复制模板表单并删除旧表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!