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

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

问题描述

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

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();
}

无论我在哪里使用它,它都会产生 #ERROR!.当我把光标放在上面时,它说:

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

错误:ReferenceError:未定义今天".

error : ReferenceError: "today" is not defined.

虽然 today() 函数在电子表格中直接使用时有效.

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?

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

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 是 JavaScript 的一个子集,目前不支持电子表格功能.例如,如果你想创建一个返回今天日期的函数,你应该这样写:

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 ;-)

语法与工作表函数相同:=test_today() 看教程

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

有许多关于 javascript 的互联网资源,我发现最有用的资源之一是 w3school

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

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

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