问题
I'd like to use Typescript for my Google Apps Script (GAS) projects, but I can't find a way to compile my code into something that GAS accepts.
GAS doesn't support export, and Typescript seems to dislike accessing variables via the global scope (it wants imports/require, and thus exports).
I'm looking for any of the following solutions which I believe would make things work for me:
1) Babel plugin or the like that can remove all the Import and Export statements along with their attributed names (requires that I don't use the same method names, which I don't.
So:
import MyLibrary from './library';
export function greetJohn() { MyLibrary.greet('John'); }
export default { greetJohn }
Becomes:
function greetJohn() { greet('John'); }
2) Change typescript so that it can see the global scope
3) Babel plugin or the like that combines all the .ts files into one .js file and transforms the import/export statements by treating each file like an Object/function.
回答1:
Now it's possible to use Typescript to develop Google Apps Script projects but this could not be done directly on the Apps Script Editor.
According to Develop Apps Script using TypeScript the easier way is by using clasp.
Related
- Enabling autocomplete for Google Apps Script in locally-installed IDE
回答2:
I've been looking into a similar scenario this past couple of weeks (not using TypeScript but still ES6/ES7).
Some of the things I've found that you might find helpful for what you're trying to achieve:
- GAS webpack plugin allows you to use webpack for module loading within GAS by detecting when you're assigning to the global object and then generating a top level function which GAS can run. This means your import and exports will all be handled by webpack so you don't have to remove them.
- I wasn't able to get
import * as x from y
syntax to work howeverimport { x } from y
andimport x from y
worked fine when using webpack. - You can include your HTML as a string in your bundle using HTML loader.
If you don't want to use webpack, one solution is to put all your code in a single app.ts
file, create an object containing all of your functions, set the functions to be top-level so they can be picked up by GAS. You could also export the container object and use it in a test suite. When you compile with Babel use the babel-plugin-transform-remove-export plugin to remove the export statement.
const app = {
onInstall: () => { ...
},
onOpen: () => { ...
}
}
const { onOpen, onInstall } = app;
export { app };
来源:https://stackoverflow.com/questions/48791868/use-typescript-with-google-apps-script