Skip to content

Commit 8ff174f

Browse files
committed
Added Support for Custom Sheet Functions
1 parent 8738410 commit 8ff174f

File tree

8 files changed

+1013
-1099
lines changed

8 files changed

+1013
-1099
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
dist
22
node_modules
3-
google_apps_script
3+
google_apps_script
4+
functions

FUNCTIONS.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Custom Functions in Google Sheets
2+
3+
Google Sheets has a number of built-in formula functions such as `=SUM()` that enable quick calculations on spreadsheet data. Custom functions are simply functions that you define yourself, using Apps Script. Once you've defined a custom function, you can use it anywhere in your spreadsheet, just like a built-in function.
4+
5+
This section shows you how to create a custom Sheet functions locally and upload it to your Apps Script project.
6+
7+
1. Create a new file in the function folder and append a .js extension to the file name.
8+
9+
2. Write the sheets function in the new file and append a JSDoc comment blocks to explain what your function will do. [See example](./functions/currency.js)
10+
11+
In this comment, you can identify two parts: the function description and annotations that describe the function's parameters and return type.
12+
13+
`@param:` You can use the @param annotation to describe each parameter passed into the function.
14+
15+
`@return:` You can utilize the @return annotation to describe what the function returns.
16+
17+
`@customfunction:` You should always add `@customfunction` in any custom function's doc comment. This annotation notifies Sheets to autocomplete your custom function just as Sheets autocomplete built-in functions when you type a function name in a cell as seen below:
18+
19+
Save the file. The functions file will automatically get uploaded to the Apps Script project with the comment block intact.
20+
21+
The comment block is required for Google Sheets to autocomplete for custom functions much like for built-in functions. As you type a function name in a cell, you will see a list of built-in and custom functions that matches what you enter.
22+
23+
### Guidelines for Naming custom functions
24+
25+
Before writing your own custom function, there are a few guidelines to know.
26+
27+
- The name of a custom function must be distinct from the names of built-in functions like SUM().
28+
- The name of a custom function cannot end with an underscore (\_), which denotes a private function in Apps Script.
29+
- The name of a custom function must be declared with the syntax function myFunction(), not var myFunction = new Function().
30+
- Capitalization does not matter, although the names of spreadsheet functions are traditionally uppercase.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ The default `.claspignore` file in the Apps Script Starter kit will push all the
7878

7979
Create a new repository in Github and make a note of the URL of the new repository. Next, open the terminal and run the above commands to push your Apps Script project to Github.
8080

81+
## Custom Google Sheet function
82+
83+
Please read [the tutorial](./FUNCTIONS.md) on how to write custom functions for Google Sheets using Apps Script.
84+
8185
## :fire: Meet the Developer
8286

8387
<img align="left" width="100" height="100" src="https://pbs.twimg.com/profile_images/1144978512832368640/Ej7Zz7E9_400x400.jpg">

functions/currency.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Converts US dollars to Indian Rupee.
3+
*
4+
* @param {number} dollars The total number of dollars.
5+
* @return {number} indianRupee The converted total of Indian Rupeet.
6+
* @customfunction
7+
*/
8+
function USDTOINR(dollars){
9+
var indianRupee = dollars * 76;
10+
return indianRupee;
11+
}

functions/double.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Multiplies the input value by 2.
3+
*
4+
* @param {number} input The value or range of cells to multiply.
5+
* @return The input multiplied by 2.
6+
* @customfunction
7+
*/
8+
function DOUBLE(input) {
9+
if (input.map) {
10+
return input.map(DOUBLE); // Recurse over array if so.
11+
} else {
12+
return input * 2;
13+
}
14+
}
15+
16+

package-lock.json

Lines changed: 933 additions & 1086 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,29 @@
2929
"crypto-js": "3.1.9-1"
3030
},
3131
"devDependencies": {
32-
"@babel/core": "^7.10.2",
33-
"@babel/plugin-proposal-object-rest-spread": "^7.10.1",
34-
"@babel/plugin-proposal-optional-chaining": "^7.10.1",
35-
"@babel/preset-env": "^7.10.2",
32+
"@babel/core": "^7.10.3",
33+
"@babel/plugin-proposal-object-rest-spread": "^7.10.3",
34+
"@babel/plugin-proposal-optional-chaining": "^7.10.3",
35+
"@babel/preset-env": "^7.10.3",
3636
"@google/clasp": "^2.3.0",
3737
"@types/google-apps-script": "^1.0.14",
3838
"babel-eslint": "^10.1.0",
3939
"babel-loader": "^8.1.0",
4040
"babel-plugin-add-module-exports": "^1.0.2",
4141
"clean-webpack-plugin": "^3.0.0",
42-
"copy-webpack-plugin": "^6.0.1",
42+
"copy-webpack-plugin": "^6.0.2",
4343
"cross-env": "^7.0.2",
44-
"eslint": "^7.1.0",
45-
"eslint-config-airbnb-base": "^14.1.0",
44+
"eslint": "^7.3.1",
45+
"eslint-config-airbnb-base": "^14.2.0",
4646
"eslint-config-prettier": "^6.11.0",
4747
"eslint-loader": "^4.0.2",
4848
"eslint-plugin-googleappsscript": "^1.0.3",
49-
"eslint-plugin-import": "^2.20.2",
50-
"eslint-plugin-prettier": "^3.1.3",
51-
"gas-webpack-plugin": "^1.0.2",
49+
"eslint-plugin-import": "^2.22.0",
50+
"eslint-plugin-prettier": "^3.1.4",
51+
"gas-webpack-plugin": "^1.0.3",
5252
"prettier": "^2.0.5",
53-
"terser-webpack-plugin": "^3.0.2",
53+
"terser-webpack-plugin": "^3.0.6",
5454
"webpack": "^4.43.0",
55-
"webpack-cli": "^3.3.11"
55+
"webpack-cli": "^3.3.12"
5656
}
5757
}

webpack.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ module.exports = {
8383
from: `${src}/../appsscript.json`,
8484
to: destination,
8585
},
86+
{
87+
from: `${src}/../functions/*.js`,
88+
to: destination,
89+
flatten: true,
90+
},
8691
],
8792
}),
8893
new GasPlugin({

0 commit comments

Comments
 (0)