我们为什么要使用SpreadsheetApp.flush();? [英] Why do we use SpreadsheetApp.flush();?

查看:271
本文介绍了我们为什么要使用SpreadsheetApp.flush();?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个完整的初学者。

我阅读Google文档以获得解决方案。
我在网上搜索了相同的内容。
我也搜索出了相同的StackOverflow。



但是。一切似乎都是技术性的。


我的理解是,.Flush有助于在不发生捆绑的情况下执行函数。 / p>

对吗?如果不是,通俗的说法是什么意思?请举个简单的例子。谢谢。

解决方案

程序员将使用 flush()当他们想要确保在继续之前先前代码的输出和/或效果被写入电子表格。如果你没有 flush(),那么代码可以通过使用一些内置的缓存和操作绑定来自动优化。一般来说,直到你明确指定 DO 需要...时,你才需要使用 flush()



首先,您可以使用官方文档:


flush()



应用所有待处理的电子表格更改。电子表格操作有时会捆绑在一起以提高性能,例如对Range.getValue()进行多次调用时。但是,有时您可能希望确保所有待处理的更改都立即生效,例如,将脚本正在执行的用户数据显示出来。


如何 explainlikeimfive 比喻:比方说,你正在用一棵树来计算苹果100个苹果。



您可以单独计算并记录每个苹果,如下所示:



< 1 2 3 4 5 6 ...等。



这就像在循环中做一个 flush(),因为在每个 count之后操作。你最终会写下你的论文 100次,假设用你的手写作需要更长的时间,而不是用你的眼睛来计算。



一个优化的过程(在这种情况下)将是使用你的内存/缓存并在写下一个数字之前计算5个苹果,所以你可以写出

5 10 15 20 ...等等。



现在您最终会写信给您的论文 20次减少80%),尽管必须计算相同数量的苹果,但您减少了必须执行的写入次数,因此您将通过减少运行时间的方式获得巨大的性能优势。



这大致转化为Apps脚本操作的工作方式。与所有计算一样,内存中操作最快执行,而读/写(又名输入/输出或I / O)操作是最慢的(请检查您的执行记录作进一步证明)。这就是为什么当你特别需要在代码执行的特定时刻将数据写入电子表格时,你应该只使用 flush()


希望这有助于。


I'm a complete beginner.

I read the Google Document for the solution. I searched out the internet for the same. I also searched out StackOverflow for the same.

But. Everything seems technical.

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. Thanks.

解决方案

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/cache 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, or I/O) 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.

这篇关于我们为什么要使用SpreadsheetApp.flush();?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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