如何在Google表格中隐藏Google App脚本功能?

huangapple go评论62阅读模式
英文:

How to hide Google App Script function in Google Sheet?

问题

在Google应用脚本项目中,我创建了一个用于获取令牌的函数。在该项目中,它正常工作。但我注意到我的函数在Google Sheets中是可见的,并且它在Google表格单元格中返回令牌。我已经附上了图片。

--> 您可以看到我已经定义了用于获取令牌的getTokenFromMacro函数。

--> 在这里,您可以看到我的函数在Google表格单元格中是可见的。

那么,我如何隐藏我的令牌以防止其他人看到,或者如何隐藏我的函数以使其不可见于Google Sheets函数中?

我在javascript.html文件中使用了我的getTokenFromMacro。

是否有解决这个问题的方法?

英文:

In the Google app script project, I created one function for getting a token. It's working fine perfectly in the project. but I noticed that my functions are visible in Google Sheets. And it's returning the token in the Google sheet cell. I had attached the image for that.

--> You can see that I had defined the getTokenFromMacro function for getting token.

如何在Google表格中隐藏Google App脚本功能?

--> Here you can see that my functions are visible in the google sheet cell.

如何在Google表格中隐藏Google App脚本功能?

So how can I hide my token not to visible to others or how can I hide my function to not to be visible in Google Sheets function?

I used my getTokenFromMacro in a javascript.html file.
如何在Google表格中隐藏Google App脚本功能?

Is there any solution to this problem?

答案1

得分: 3

Google Apps Script 允许您指定函数是私有的还是可以通过 Google Sheets 访问的:

自定义函数的名称不能以下划线(_)结尾,下划线表示 Apps Script 中的私有函数。

您可以将您的函数声明更改为:

function getTokenFromMacro_() {
  const scriptProperties = PropertiesService.getScriptProperties();
  const token = scriptProperties.getProperty('API_TOKEN')
  return token;
}

当尝试将其用作自定义函数时,将出现错误 Unknown function: 'getTokenFromMacro_'

如何在Google表格中隐藏Google App脚本功能?


参考资料:

  • 自定义函数指南 - 命名

英文:

Google Apps Script allows you to specify if the function is private or if it will be accessible via Google Sheets:

>The name of a custom function cannot end with an underscore (_), which denotes a private function in Apps Script.

You can change your function declaration to:

function getTokenFromMacro_() {
  const scriptProperties = PropertiesService.getScriptProperties();
  const token = scriptProperties.getProperty('API_TOKEN')
  return token;
}

The error Unknown function: 'getTokenFromMacro_'. will appear when trying to use it as a custom function:

如何在Google表格中隐藏Google App脚本功能?


Reference:

  • Guidelines for custom functions - Naming

答案2

得分: 0

请尝试:从HTML中调用一个名为nameYouWant()的公共函数,该函数将调用一个名为getTokenFromMacro_()的私有函数。

在GAS中:

function nameYouWant(){
 return getTokenFromMacro_()
}

在HTML中:

google.script.run
 .withFailure...
 .withSuccess...
 .withUser...
 .nameYouWant()
英文:

Please, try it: from HTML you'll call a public function - nameYouWant() - and that fucntion will call a private function - getTokenFromMacro_()

in GAS

function nameYouWant(){
 return getTokenFromMacro_()
}

in HTML

google.script.run
 .withFailure...
 .withSuccess...
 .withUser...
 .nameYouWant()

huangapple
  • 本文由 发表于 2023年6月6日 17:06:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76413040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定