为什么我们要使用vredrathapp.flush()?
我了解的是, .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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
程序员将使用
flush()
要确保在继续之前将上一个代码的输出和/或效果写入电子表格时。如果您不flush()
,则可以使用一些内置的缓存和操作捆绑来自动“优化”代码。通常,您不需要使用flush()
,直到您具体 do 需要...如果有意义。首先,Ye Olde官方文件:
like likeimfive ableOgh 类比:假设您要在树上计算100个苹果在树上的苹果。
您可以计数并单独记录每个苹果,例如:
1
,2
,3
,4
,5
,6
...等。每次
计数之后编写
操作。您最终将写入纸张 100次,并假设用手写作要比用眼睛算得更长的时间。“优化”过程(在这种情况下)是在写下一个数字之前使用您的内存/缓冲区,并计算5个苹果,因此您要写
5
,10
,15
,20
...等。必须计算相同数量的苹果,您已经减少了写作数量您必须这样做,因此您会看到通过减少运行时的巨大性能优势。
这大致转化为应用程序脚本操作的工作方式。与所有计算中一样,内存操作是最快执行的,并且读取/写入(又称输入/输出)操作是最慢的(检查您的执行成绩单以获取进一步的证明)。这就是为什么您只需要在代码执行中的特定点将数据写入电子表格时才应使用
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 notflush()
, then the code may be automatically "optimized" by using some built-in caching and bundling of operations. In general, you do not need to useflush()
until you specifically DO need to... if that makes sense.First, ye olde official documentation:
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 eachcount
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.