Skip to content

Commit

Permalink
Merge pull request #3 from iansan5653/update-docs-exports-bug
Browse files Browse the repository at this point in the history
Update docs to work around bug in `gas-webpack-plugin`
  • Loading branch information
iansan5653 authored May 20, 2024
2 parents 54788e9 + 6104cdb commit eaf82bb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
4 changes: 1 addition & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,31 @@ In order for your Google Apps to run any of your code, you'll need to expose one
to the engine. In the traditional Google Apps Script environment, you'd do this by declaring global
functions, however in this setup there is no concept of 'global' as all files are modules.

Instead, all (and only) functions exported from `index.ts` will be available to Google Apps Script.
Instead, only functions exported from `index.ts` will be available to Google Apps Script.
Any function exported from `index.ts` will be accessible by all
[triggers](https://developers.google.com/apps-script/guides/triggers) and anywhere else Google Apps
might need to call your function, such as from a
[custom menu](https://developers.google.com/apps-script/guides/menus).

Due to [a bug](https://github.com/iansan5653/gas-ts-template/issues/2) in the Webpack plugin, only exports in `export {...}` form are supported:

```ts
// ❌ Does NOT work
export function bad1() { /* ... */ }

export const bad2 = () => { /* ... */ }

// ✅ Does work:
export {good1} from "./good1.ts"

function good2() { /* ... */ }
const good3 = () => { /* ... */ }

export {good2, good3}
```

Examples for all the simple triggers are given in
[`index.ts`](https://github.com/iansan5653/gas-ts-template/blob/master/src/index.ts).
[`index.ts`](./src/index.ts).

For cleaner, more usable code, it may be useful to reference functions by their `name` property instead of hardcoding the name into code.
For example:
Expand Down
4 changes: 3 additions & 1 deletion src/example.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const helloWorld = "Hello, world!";
export function helloWorld() {
console.log("Hello, world!");
}
21 changes: 12 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
// You can access any of the global GAS objects in this file. You can also
// import local files or external dependencies:
import { helloWorld } from "./example";

console.log(helloWorld);
export { helloWorld } from "./example";

// Simple Triggers: These five export functions are reserved export function names that are
// called by Google Apps when the corresponding event occurs. You can safely
// delete them if you won't be using them, but don't use the same export function names
// for anything else.
// See: https://developers.google.com/apps-script/guides/triggers

export function onOpen(
// NOTE: only `export {...}` syntax will work. You cannot define and export a trigger in
// the same line.

function onOpen(
e:
| GoogleAppsScript.Events.DocsOnOpen
| GoogleAppsScript.Events.SlidesOnOpen
| GoogleAppsScript.Events.SheetsOnOpen
| GoogleAppsScript.Events.FormsOnOpen
| GoogleAppsScript.Events.FormsOnOpen,
): void {
console.log(e);
}

export function onEdit(e: GoogleAppsScript.Events.SheetsOnEdit): void {
function onEdit(e: GoogleAppsScript.Events.SheetsOnEdit): void {
console.log(e);
}

export function onInstall(e: GoogleAppsScript.Events.AddonOnInstall): void {
function onInstall(e: GoogleAppsScript.Events.AddonOnInstall): void {
console.log(e);
}

export function doGet(e: GoogleAppsScript.Events.DoGet): void {
function doGet(e: GoogleAppsScript.Events.DoGet): void {
console.log(e);
}

export function doPost(e: GoogleAppsScript.Events.DoPost): void {
function doPost(e: GoogleAppsScript.Events.DoPost): void {
console.log(e);
}

export { onOpen, onEdit, onInstall, doGet, doPost };

0 comments on commit eaf82bb

Please sign in to comment.