在Google文档/电子表格的自定义公式中使用现有的电子表格公式 [英] Use existing spreadsheet formulas in a custom formula in google docs/spreadsheets

查看:86
本文介绍了在Google文档/电子表格的自定义公式中使用现有的电子表格公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢在Google文档电子表格中编写自己的公式.但是我经常想做的事情与已经存在的功能非常相似.例如,我找不到将日期(2010年8月31日)转换为一周中的词汇日(星期二)的函数.我想写:

I like writing my own formulas inside of Google Docs Spreadsheets. But often what I want to do is very similar to a function that already exists. As an example, I couldn't find a function to turn a date (31-Aug-2010) into the lexical day of the week (Tuesday). I'd like to write:

=LexWeekDay('31-Aug-2010')
'Tuesday'

很明显,我可以使用核心javascript编写所有这些逻辑,但是已经有一个名为 WEEKDAY() normal 电子表格函数,其中

Clearly I can write all of this logic using core javascript, but there already exists a normal spreadsheet function called WEEKDAY() which takes a date and converts into into a number representing the day of the week [0 => Sunday, 1=> Monday, etc].

如何从我的自定义脚本访问该spedsheets已定义的此功能(或通常的任何功能)?

推荐答案

在自定义应用程序脚本中,您可以通过以下方式使用google电子表格的内置公式:

In your custom appscript, you can use the in-built formulas of google spreadsheet in this way:

假设您要在单元格A1上使用 = WEEKDAY()函数.

Lets say you want to use =WEEKDAY() function on cell A1.

然后,在自定义应用程序脚本功能中获取如下所示的活动电子表格:

Then, get your active spreadsheet like this in your custom appscript function:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("YOUR_SHEET_NAME");

现在,像这样设置公式:

now, set the formula like this:

sheet.getRange("A1").setValue("=WEEKDAY()");

此外,如果要将0,1等转换为星期日,星期一...,那么请定义一个这样的数组:

Also, if you want to convert 0,1 etc to Sunday,Monday...then define an array like this:

var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];

然后使用:

var dayIndex = sheet.getRange("A1").getValue(); 
Logger.log(days[dayIndex]);

您可以使用ctrl + enter或通过脚本编辑器本身中的查看"->日志"来查看日志.

You can see the logs using ctrl+enter or by going to View->Logs in script editor itself.

这篇关于在Google文档/电子表格的自定义公式中使用现有的电子表格公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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