在脚本中使用内置的电子表格函数 [英] Using built-in spreadsheet functions in a script

查看:90
本文介绍了在脚本中使用内置的电子表格函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用Google App Script。
我在Google Doc电子表格中使用它。



我正在尝试非常简单的函数,只是为了学习基础知识。例如,这可以工作:

  function test_hello(){
return'hello';
}

但是我很困惑这个简单的问题:

  function test_today(){
return today();
}

它使 #ERROR!无论我在哪里使用它。
当我将光标放在它上面时,它说:

 错误:ReferenceError:today未定义。 

虽然 today()直接在电子表格中使用。

这是否意味着在脚本中,我不能使用电子表格内置函数?
是否有任何优雅的方法呢?



一些电子表格功能对我来说非常有用(例如,我喜欢 weekday())。



一种非优雅的方式可能是创建列来计算我需要的中间值,并且可以使用电子表格函数进行计算。但我宁愿避免这种肮脏和麻烦的事情。 解决方案

Google Apps脚本是JavaScript的子集,目前不支持电子表格功能。
例如,如果你想创建一个返回今天日期的函数,你应该写:
$ b $ pre $ function test_today() {
返回新的Date()
} //注意,这将最终返回一个毫秒值,你必须将单元格格式设置为'date'或'time'或者两者;-)

语法与表单函数相同: = test_today() code> 查看教程



在javascript上有很多互联网资源,我发现其中一个最有用的是 w3school


I'm using Google App Script for the first time. I'm using it on a Google Doc spreadsheet.

I'm trying very simple functions, just to learn the basics. For example this works:

function test_hello() {
    return 'hello';
}

But I'm puzzled by this simple one :

function test_today() {
    return today();
}

It makes an #ERROR! wherever I use it. And when I put my cursor on it, it says :

error : ReferenceError: "today" is not defined.

While the today() function works when used directly in the spreadsheet.

Does this mean that in scripts, I cannot use spreadsheet built-in functions? Is there any elegant way around this?

Some spreadsheet functions are quite useful to me (I like weekday() for example).

A non-elegant way could be to create columns to calculate intermediate values that I need, and that can be calculated with spreadsheet functions. But I'd rather avoid something this dirty and cumbersome.

解决方案

Google Apps Script is a subset of JavaScript, spreadsheet functions are currently not supported. For example, if you want to create a function that returns today's date you should write :

function test_today(){
return new Date()
}// note that this will  eventually return a value in milliseconds , you'll have to set the cell format to 'date' or 'time' or both ;-)

syntax is the same as with sheet functions : =test_today() see tutorial

There are many internet ressources on javascript, one of the most useful I found is w3school

这篇关于在脚本中使用内置的电子表格函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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