在GAS中使用Global vars是否可取?有什么缺点吗? [英] Using Global vars in GAS, is it advisable? are there any downsides?

查看:76
本文介绍了在GAS中使用Global vars是否可取?有什么缺点吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果这是一个愚蠢的问题,但这是可行的

Sorry if this a stupid question, but here goes

所以我一直在学习与Google Application Scripts一起使用的Java语言大约一年了,但是我确实会慢慢找到自己的脚.

So I've been learning Javascript for use with Google Application Scripts for about a year now and slowly but surely am finding my feet.

我试图帮助某人在这里写他们的脚本,我注意到他们宣告了

I tried to help someone with their script on here, and i notice that they declared

var ss = SpreadSheetApp.getActiveSpreadsheet();

在脚本的最顶部,在所有功能之外,作为全局变量.

at the very top of the script, outside all functions, as a global var.

我想,当我为电子表格编写多个函数/脚本时,可能有必要在全局声明一些VAR,而不是在不同的函数中重复它们.

It got me thinking, that when I write several functions/scripts for a spreadsheet, it might be worth declaring some VARs globally rather than repeating them in different functions.

在我盲目地走这条路之前,我认为最好先问一下在GAS中使用全局变量是否存在任何重大陷阱或问题.

Before I blindly plough down this path, I thought it best to ask are there any major pitfalls or problems with using global vars in GAS.

此外,除了在编码时节省一些键入之外,还有其他主要优点吗?

Also, are there any major advantages, besides saving a bit of typing while coding?

目前是否有人定期使用全局变量来编写GAS脚本?我想听听一切如何运作?缺点是什么,任何限制或优点.

Does anyone currently write GAS scripts, regularly using global vars. I'd be interested to hear how it all works? What the cons are, any limitations, or advantages.

在此行下方进行编辑

只需添加一下,我一直在做的95%的工作仅限于Google表格,并且使用了lil gmail脚本.到目前为止,这就是我的范围.最好提一下,因为我真的没有想到其他Google产品的脚本的含义.

Just wanted to add, 95% of the things I've been doing have been confined to Google Sheets, with a lil gmail scripting. So that's my scope so far. Thought it best to mention, as I don't really have the implications for scripts for other Google products in mind.

推荐答案

当您使用变量定义(例如在示例中)调用Google服务时,必须意识到每次运行时都会执行此调用项目中的任何功能.

When you use a variable definition such as in your example that calls a Google Service you have to be conscious that this call will be executed each time you run any function in your project.

这意味着即使就代码编写而言可能是高效的(我想我们都是懒惰的人),但就执行速度而言,它实际上还是无效的.

This means that even if it's probably efficient in terms of code writing (we are all lazy guys I guess) it is really not efficient in terms of execution speed.

相反,让我们假设您执行了一个不需要ss变量的函数,SpreadSheetApp.getActiveSpreadsheet()将被执行...

On the contrary, let's imagine you execute a function that does not need the ss variable, the SpreadSheetApp.getActiveSpreadsheet() will be executed...

使用这样的简单调用可能不是问题,但是如果将这种情况乘以许多不同的变量来调用许多不同的服务,则最终会降低您的执行速度.

With a simple call like that it might not be an issue but if you multiply this case for a lot of different variables calling many different services it will finally slow down your execution speed.

我不是一个真正的"程序员,所以我的观点仅基于个人习惯和发现,因为我从没学过任何学术性"知识.也许其他人会有一个更相关的答案.

I'm not a 'real' programmer, so my opinion is only based on personal habits and findings since I never learned anything "academic" in that matter. Maybe someone else will have a more relevant answer.

当我键入Jonathon时,就这样做了!我当然同意他的意见,我将在这里只留一些关于效率的内容;-)

这篇关于在GAS中使用Global vars是否可取?有什么缺点吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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