diff --git a/package-lock.json b/package-lock.json index 6a3e4e0..1ba694e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,10 @@ { - "name": "your-project-name", + "name": "gas-ts-template", "version": "1.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "your-project-name", - "version": "1.0.0", "license": "GPL", "devDependencies": { "@google/clasp": "^2.4.2", diff --git a/readme.md b/readme.md index c011b2c..770d9eb 100644 --- a/readme.md +++ b/readme.md @@ -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: diff --git a/src/example.ts b/src/example.ts index 285c597..b0528dc 100644 --- a/src/example.ts +++ b/src/example.ts @@ -1 +1,3 @@ -export const helloWorld = "Hello, world!"; +export function helloWorld() { + console.log("Hello, world!"); +} diff --git a/src/index.ts b/src/index.ts index f85cf06..2250e24 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,6 @@ // 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 @@ -10,28 +8,33 @@ console.log(helloWorld); // 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 };