简体   繁体   English

在 Google 表格中,如果起始工作表的尺寸是动态的,如何使用脚本将选项卡复制到 SAME 工作簿中的另一个选项卡?

[英]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:需要的标准:

  1. The copy-and-pasting should preferably not rely on fixed row and column figures, but rather be a select-all operation, so that if any changes are made to the template, they will be incorporated automatically the next time the script runs, even if the number of rows and columns in the template changes.复制粘贴最好不要依赖固定的行列图,而是全选操作,这样如果对模板进行了任何更改,它们将在下次脚本运行时自动合并,即使如果模板中的行数和列数发生变化。
  2. The process should erase any possible errors or damage done to the visible sheet by users who can't follow instructions.该过程应消除无法遵循说明的用户对可见工作表造成的任何可能的错误或损坏。
  3. The process should incorporate the conditional formatting of the hidden template, so that it doesn't have to be re-encoded nightly.该过程应包含隐藏模板的条件格式,因此不必每晚重新编码。

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:它似乎不存在,至少不能满足我的需要,因为:

  1. It can't rely on an continually-updating, dynamic function like IMPORTRANGE;它不能像 IMPORTRANGE 那样依赖于不断更新的动态 function; the whole thing already runs about as slowly as people can tolerate, and anything that increase the processing load would be unacceptable,整个事情已经以人们可以容忍的速度运行,任何增加处理负载的事情都是不可接受的,
  2. I don't think that copying between multiple Google Sheets workbooks currently copies conditional formatting, which would really be helpful, and我认为当前在多个 Google 表格工作簿之间进行复制不会复制条件格式,这确实很有帮助,并且
  3. It seems like the copy-to-another-workbook functions all create a new sheet, which would then run into the problem of the existing template tab still being the source, not the new tab, so that manual changes to every workbook would still be required.似乎复制到另一个工作簿的功能都创建了一个新工作表,然后会遇到现有模板选项卡仍然是源而不是新选项卡的问题,因此对每个工作簿的手动更改仍然是必需的。 If anyone has any ideas how to move in that direction, I'm all ears, but only solving the first problem is essential for the time being.如果有人对如何朝着这个方向发展有任何想法,我会全力以赴,但目前只解决第一个问题是必不可少的。 Thank you to anyone who can offer input!感谢任何可以提供意见的人!
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.

相关问题 如何使用 zapier 自动将填充有 zapier 的新行的值复制到具有 google 工作表的同一工作簿中的另一个工作表? - How to automaticaly copy values of a new row populated with zapier to another sheet in same workbook with google sheets? Google 表格脚本,如何创建工作表标签的新松散 object - Google Sheets Script, how to create a NEW loose object of a sheet tab 如何将一张工作表的值复制到另一张工作表Google工作表脚本 - How to copy the value of one sheet to another sheet Google sheets Script 将数据从一个 Google 工作表工作簿工作表复制到另一个工作簿 - Copy Data from One Google sheets Workbook Sheet to another Workbook Google Sheets Script -> 如何根据单元格触发器将行作为值复制到另一个选项卡中,然后清除触发器单元格? - Google Sheets Script -> How to copy row as values into another tab based on a cell trigger and then clear the trigger cell? 用于将范围复制到另一张工作表的 Google 表格脚本 - Google Sheets script to copy a range to another sheet 我可以使用脚本以当前数据作为选项卡名称复制和重命名 Google 表格中的工作表吗? - Can I use a script to copy and rename a sheet in Google Sheets with the current data as the tab name? 如何从一个Google工作表标签复制/粘贴到另一个Google工作表并指定目标Google工作表中的标签? - How do you copy/paste from one Google Sheet Tab, to another google sheet and specify the tab in the target google sheet? 将一个标签上的谷歌工作表中的值复制到另一个标签 - Copy values from google sheet on one tab to another tab 如何使用 Google App Script 将一个 Google Sheet 中的所有数据提取到另一个 Google Sheet Tab? - How do I pull All data in one Google Sheet to another Google Sheet Tab using Google App Script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM