为什么我们要使用vredrathapp.flush()?

发布于 2025-02-13 09:46:15 字数 289 浏览 2 评论 0原文

我了解的是, .flush()代码>有助于执行函数时执行这些函数,而无需将它们捆绑在一起。

我对吗?如果没有,外行术语中的含义是什么?请以一个简单的例子。

What I understand is, .flush() helps to execute the functions as and when they happen without bundling them in one.

Am I right? If not, what's the meaning in layman terms? And please, with a simple example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的那么颓废 2025-02-20 09:46:16

程序员将使用flush()要确保在继续之前将上一个代码的输出和/或效果写入电子表格时。如果您不flush(),则可以使用一些内置的缓存和操作捆绑来自动“优化”代码。通常,您不需要使用flush(),直到您具体 do 需要...如果有意义。

首先,Ye Olde官方文件:

flush()

应用所有等待的电子表格更改。电子表格操作有时会捆绑在一起以提高性能,例如在对range.getValue()进行多个呼叫时。但是,有时您可能需要确保立即进行所有待处理更改,例如,在脚本执行时向用户显示数据。

like likeimfive ableOgh 类比:假设您要在树上计算100个苹果在树上的苹果。

可以计数并单独记录每个苹果,例如:

123456 ...等

。每次计数之后编写操作。您最终将写入纸张 100次,并假设用手写作要比用眼睛算得更长的时间。

“优化”过程(在这种情况下)是在写下一个数字之前使用您的内存/缓冲区,并计算5个苹果,因此您要写

5101520 ...等

。必须计算相同数量的苹果,您已经减少了写作数量您必须这样做,因此您会看到通过减少运行时的巨大性能优势。

这大致转化为应用程序脚本操作的工作方式。与所有计算中一样,内存操作是最快执行的,并且读取/写入(又称输入/输出)操作是最慢的(检查您的执行成绩单以获取进一步的证明)。这就是为什么您只需要在代码执行中的特定点将数据写入电子表格时才应使用flush()

希望这会有所帮助。

A programmer will use flush() when they want to ensure that the previous code's output and/or effects are written to the spreadsheet before continuing. If you do not flush(), then the code may be automatically "optimized" by using some built-in caching and bundling of operations. In general, you do not need to use flush() until you specifically DO need to... if that makes sense.

First, ye olde official documentation:

flush()

Applies all pending Spreadsheet changes. Spreadsheet operations are sometimes bundled together to improve performance, such as when doing multiple calls to Range.getValue(). However, sometimes you may want to make sure that all pending changes are made right away, for instance to show users data as a script is executing.

How about an explainlikeimfive analogy: Let's say you're counting apples on a tree with 100 apples.

You could count and record each apple individually, like so:

1, 2, 3, 4, 5, 6... etc.

This is like doing a flush() within a loop, since you are literally writing after each count operation. You will end up writing to your paper 100 times, and let's assume it takes longer to write with your hand than it does to count with your eyes.

An "optimized" process (in this case) would be to use your memory/buffer and count 5 apples before writing a number down, so you'd write

5, 10, 15, 20... etc.

Now you will end up writing to your paper 20 times (an 80% reduction), and despite having to count the same number of apples, you've reduced the number of writes you have to do, so you'll see a drastic performance benefit by way of reduced runtime.

This translates roughly to how Apps Script operations work. As in all computing, the in-memory operations are the quickest to execute, and the read/write (aka input/output) operations are the slowest (check your Execution Transcript for further proof). That's why you should only use flush() when you specifically need to write your data to the spreadsheet at a particular point in your code's execution.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文