setValues比setValue慢吗? [英] Is setValues slower than setValue?

查看:51
本文介绍了setValues比setValue慢吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行了Google Apps Script脚本的Google Doc,有多个人参与其中。人们编辑工作表时会触发一个 onEdit 触发器。据我了解,如果一个人编辑工作表,则会触发我的 onEdit 触发函数。

I've got a Google Doc with Google Apps Script script running that multiple people contribute to. There is an onEdit trigger that is fired when people edit the sheet. As I understand it, if one person edits the sheet this triggers my onEdit trigger function.

如果另一个人之前编辑了工作表第一个 onEdit 完成处理,然后停止第一个实例,第二个 onEdit 开始而不完成第一个实例。

If a second person edits the sheet before the first onEdit finished processing then it stops the first instance and the second onEdit starts without finishing the first one.

为避免这种情况,我尝试通过以下理论使代码尽可能高效: onEdit 进程花费的时间更少可能会重叠。为此,我尝试尽量减少读写。通过使用一系列记录器语句,我跟踪了很耗时的代码行。我注意到,我的 setValues()之一运行将近6秒钟。在我对其进行调整之后,这是一个很大的数组。

To avoid this I'm trying to make my code as efficient as possible with the theory that if the onEdit process takes less time it's less likely to overlap. To accomplish this I've tried to minimize my read/writes. Using a series of logger statements I've tracked down lines of code that are time consuming. I've noticed that one of my setValues() lines takes almost 6 seconds to run. It's a large array being posted back to the sheet after I've made adjustments to it.

我的问题:


  1. setValues()调用所需的时间是否比 setValue()所需的时间更长?

  2. 对小数组的 setValues()调用比大数组的 setValues()花费的时间更少?

  1. Does a setValues() call take longer than a setValue()?
  2. Does a setValues() call of a small array take less time than a setValues() of a large array?

我知道一个 setValues()会比1000 <$ c $快得多c> setValue()命令。

I know that one setValues() will be much faster than 1000 setValue() commands.

我假定一次写入工作表应该花费相同的时间,而不管大小如何。我有什么办法可以最小化这次的时间?

I had presumed one write to the sheet should take about the same amount of time regardless of size. Is there any way I can minimize this time?

推荐答案


问题1:请问 setValues() setValue()需要更长的时间?

A1:不是。关于这一点,您可以从以下实验结果中看到。

A1: It's no. About this, you can see from the following experimental result.


第二季度:对小数组的 setValues()调用花费的时间少于 setValues()大型数组?

Q2: Does a setValues() call of a small array take less time than a setValues() of a large array?

A2:是的。关于这一点,您可以从以下实验结果中看到。

A2: It's yes. About this, you can see from the following experimental result.


第3季度:我认为无论大小如何,一次写入工作表应花费大约相同的时间。 A3:当您希望减少用于放置更多值的过程成本时,如何使用Sheets API,请问如何最大限度地减少这次时间?

Q3: I had presumed one write to the sheet should take about the same amount of time regardless of size. Is there any way I can minimize this time?

A3: ?对此,您可以在 A2处看到它。

A3: When you want to reduce the process cost for putting the values more, how about using Sheets API? About this, you can see it at the figure of "A2".

@TheMaster的评论,我还认为您的问题可以通过 LockService

As @TheMaster's comment, I also think that your issue might be resolved by LockService.


  • 我认为LockService的示例脚本可以在stackoverflow上看到。 参考

  • I think that the sample script of LockService can be seen at the stackoverflow. Ref

这篇关于setValues比setValue慢吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆