[英]In Google Sheets, how to copy tab to another tab in SAME workbook using Script, if the dimensions of the starting sheet are dynamic?
Background & goal:背景与目标:
I'm using multiple Google Sheets that are each an interactive interface for multiple users, with various triggers and functions built in. The problem is that, even with instructions, highlighting, and protected ranges, people still find ways to break functions on the sheet.我正在使用多个 Google 表格,每个表格都是针对多个用户的交互式界面,并内置了各种触发器和功能。问题是,即使有说明、突出显示和受保护的范围,人们仍然想方设法破坏表格上的功能. The sheet's added data needs to be copied, collated, then deleted so that the sheet is back in its original configuration for the next day's operations.需要复制、整理和删除工作表添加的数据,以便工作表恢复到其原始配置以用于第二天的操作。 My goal is to find a more efficient way to do this.我的目标是找到一种更有效的方法来做到这一点。
Current solution (clunky):当前解决方案(笨拙):
Currently, I solve this problem with an Apps Script clearRange function.目前,我使用 Apps Script clearRange function 解决了这个问题。 Every night, it takes all 4 tabs in the workbook, clears the formatting and then deletes all rows and columns until each tab is a single cell.每天晚上,它会占用工作簿中的所有 4 个选项卡,清除格式,然后删除所有行和列,直到每个选项卡都是一个单元格。 Then, the script rebuilds all four tabs, resets the conditional formatting, rewrites the cell functions, and the workbook is ready for the next day.然后,脚本重建所有四个选项卡,重置条件格式,重写单元格函数,工作簿为第二天做好准备。
The problem:问题:
The reset requires a considerable amount of code, currently 217 lines to fully reset the four tabs.重置需要相当多的代码,目前需要217 行才能完全重置四个选项卡。 The execution time is between 13 and 28 seconds and script failures with errors like Too many simultaneous invocations are not uncommon, especially since the same function is running at roughly the same time on multiple workbooks for different users.执行时间在 13 到 28 秒之间,并且脚本失败以及诸如太多同时调用之类的错误并不少见,尤其是因为相同的 function 在不同用户的多个工作簿上大致同时运行。
Desired solution:所需的解决方案:
My present thinking is that the best option is to use a hidden sheet in the workbook, or rather 4 such sheets, each being a hidden template for the four visible pages, complete with conditional formatting and whatnot.我目前的想法是,最好的选择是在工作簿中使用隐藏工作表,或者更确切地说是 4 个这样的工作表,每个工作表都是四个可见页面的隐藏模板,并带有条件格式等。 The daily reset needs to accommodate ignorant and careless errors, not malicious ones, so I'm not concerned about users hacking into and damaging the hidden template sheets.每日重置需要适应无知和粗心的错误,而不是恶意错误,所以我不担心用户侵入和破坏隐藏的模板表。
So, the ideal new script would start by cutting each sheet down to a single cell (this is still necessary to prevent errors caused by users adding or deleting rows / columns and thus breaking the reset parameters. Then, it would simply copy the cell values and conditional formatting from the template onto the visible sheet (tab). After that, only a couple lines would be needed to reinitialize some IMPORTRANGE functions and the like.因此,理想的新脚本将从将每张工作表切割成单个单元格开始(这仍然是必要的,以防止用户添加或删除行/列从而破坏重置参数而导致错误。然后,它会简单地复制单元格值和从模板到可见工作表(选项卡)的条件格式。之后,只需要几行来重新初始化一些 IMPORTRANGE 函数等。
Needed criteria:需要的标准:
So, how do I go about this?那么,我该如何处理这个问题呢? I tried using a macro, but although the copy-all-and-pasting worked when I did it the first time, the script failed to run subsequently.我尝试使用宏,但虽然我第一次复制所有并粘贴时有效,但脚本随后无法运行。 I've done searches, but all of the questions seem to be about copying from one spreadsheet to another, not doing so within a workbook, and not to an existing sheet, rather than just duplicating.我已经进行了搜索,但所有问题似乎都是关于从一个电子表格复制到另一个电子表格,而不是在工作簿中这样做,也不是复制到现有工作表,而不仅仅是复制。 I'd prefer to avoid sheet duplication, because I like having one of the visible pages being a gid=0 stable URL.我宁愿避免工作表重复,因为我喜欢其中一个可见页面是gid=0稳定的 URL。 Any help would be immensely appreciated.任何帮助将不胜感激。 If I can get this far, I'd be a happy, happy clam.如果我能走到这一步,我会是一个快乐的,快乐的蛤蜊。
Ultra long-term (bonus) goal:超长期(奖金)目标:
Beyond this, I'd ideally like to get to the point where I could have a single master template that I could change, and then with a single command push all of those changes out to the several dozen workbooks that currently (with a couple functions variable between users) exist using the same layout.除此之外,理想情况下,我希望能够拥有一个可以更改的主模板,然后使用单个命令将所有这些更改推送到当前的几十个工作簿(具有几个功能用户之间的变量)使用相同的布局存在。 As it stands right now, any changes I make to one workbook have to be encoded in the script, then that new code has to be copied into each of the dozens of copies.就目前而言,我对一个工作簿所做的任何更改都必须在脚本中进行编码,然后必须将新代码复制到数十份副本中的每一份中。 I can't tell if there's a way to make a master template that could propagate out in that way.我不知道是否有办法制作可以以这种方式传播的主模板。 It doesn't seem like there, as least not that would work properly for what I need, because:它似乎不存在,至少不能满足我的需要,因为:
function copyFromOneShtToAnother() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
const dsh = ss.getSheetByName('Sheet1');
const srg = sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn());
srg.copyTo(dsh.getRange(dsh.getLastRow() + 1, 1));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.