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

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

问题描述

我是一个完全的初学者.

I'm a complete beginner.

我阅读了 Google 文档以获取解决方案.我在互联网上搜索了相同的内容.我也在 StackOverflow 上搜索了同样的内容.

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.

我的理解是,.Flush 有助于在函数发生时执行它们,而无需将它们捆绑在一起.

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

我说得对吗?如果不是,通俗地说是什么意思?请举一个简单的例子.谢谢.

推荐答案

当程序员想要确保将先前代码的输出和/或效果写入电子表格时,他们将使用 flush()在继续之前.如果你没有flush(),那么代码可能会自动优化";通过使用一些内置的缓存和操作捆绑.通常,您不需要使用 flush(),除非您特别DO 需要......如果这有意义的话.

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:

冲洗()

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

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.

如何使用 explainlikeimfive 类比:假设您正在数一棵树上的苹果100 个苹果.

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:

123456...等

这就像在循环中执行 flush(),因为您实际上是在每个 count 操作之后编写代码.您最终会在论文上写 100 次,假设用手书写比用眼睛数需要更长的时间.

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.

优化"过程(在这种情况下)将是使用您的内存/缓冲区并在写下数字之前数 5 个苹果,所以您会写

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

5101520...等

现在,您最终将在论文上写20 次(减少 80%),尽管必须计算相同数量的苹果,但您已经减少了必须写的次数这样做,因此您会通过减少运行时间看到显着的性能优势.

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.

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

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.

希望这会有所帮助.

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

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