Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docs to work around bug in gas-webpack-plugin #3

Merged
merged 3 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
Loading