<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ryo-Nakano</title>
    <description>The latest articles on Forem by Ryo-Nakano (@ryonakano).</description>
    <link>https://forem.com/ryonakano</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F931910%2F1cb5b5b2-43ed-47ef-916c-7f104b71a346.jpeg</url>
      <title>Forem: Ryo-Nakano</title>
      <link>https://forem.com/ryonakano</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ryonakano"/>
    <language>en</language>
    <item>
      <title>Template for GoogleAppsScript development with TypeScript</title>
      <dc:creator>Ryo-Nakano</dc:creator>
      <pubDate>Sun, 06 Nov 2022 23:04:41 +0000</pubDate>
      <link>https://forem.com/ryonakano/template-for-googleappsscript-development-with-typescript-41jm</link>
      <guid>https://forem.com/ryonakano/template-for-googleappsscript-development-with-typescript-41jm</guid>
      <description>&lt;h1&gt;
  
  
  🏰 What is this article about?
&lt;/h1&gt;

&lt;p&gt;In this article, you will learn how to develop GoogleAppsScript projects in local with TypeScript by my template.&lt;/p&gt;

&lt;p&gt;▶︎▶︎▶︎ &lt;a href="https://github.com/matcher-inc/gas-template" rel="noopener noreferrer"&gt;&lt;strong&gt;Template for GoogleAppsScript development with TypeScript&lt;/strong&gt;&lt;/a&gt; ◀︎◀︎◀︎&lt;/p&gt;

&lt;p&gt;This is a MINIMUM template to keep the contents easily understandable for beginners, so anyone can easily start developing using my template.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clasp&lt;/li&gt;
&lt;li&gt;Webpack&lt;/li&gt;
&lt;li&gt;TypeScript&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These three are main components of this template.&lt;/p&gt;

&lt;h1&gt;
  
  
  👨 Who am I?
&lt;/h1&gt;

&lt;p&gt;I am a Japanese software engineer at Matcher Inc. in Japan, and have been an engineer for two years. I have written articles in Japanese before, but this is my first time to write articles in English, so it is a challenge for me.&lt;/p&gt;

&lt;p&gt;There may be parts of the English that are difficult to understand or incorrect. I would be happy if you could point them out to me.&lt;/p&gt;

&lt;h1&gt;
  
  
  🙋‍♀️ Who is it for?
&lt;/h1&gt;

&lt;p&gt;People who have experience developing in GoogleAppsScript, such as those who meet the following criteria:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Who want to use git&lt;/li&gt;
&lt;li&gt;Who want to use your favorite editor (e.g. VSCode)&lt;/li&gt;
&lt;li&gt;Who want to develop in directories divided by function&lt;/li&gt;
&lt;li&gt;Who want to use npm modules&lt;/li&gt;
&lt;li&gt;Who want to use develop with TypeScript&lt;/li&gt;
&lt;li&gt;Who want to upgrade your development experience&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are new to GoogleAppScript development, this template may be difficult to use. In that case, I recommend that you try it once you have some experience in GoogleAppsScript development.&lt;/p&gt;

&lt;h1&gt;
  
  
  🏃‍♂️ Get started
&lt;/h1&gt;

&lt;h3&gt;
  
  
  0. Set the conditions
&lt;/h3&gt;

&lt;p&gt;To proceed to the next step, must have the following ready:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create empty GoogleAppsScript project

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://script.google.com/home/start" rel="noopener noreferrer"&gt;https://script.google.com/home/start&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Enable GoogleAppsScript API

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://script.google.com/home/usersettings" rel="noopener noreferrer"&gt;https://script.google.com/home/usersettings&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Tested environment 1: Ubuntu&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ubuntu: 18.04.6 LTS
node: v16.16.0
npm: 8.18.0
clasp: 2.4.1
webpack: 5.74.0
webpack-cli: 4.10.0
TypeScript: 4.8.3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tested environment 2: Mac&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MacOS: 11.6
node: v16.17.1
npm: 8.15.0
clasp: 2.4.1
webpack: 5.74.0
webpack-cli: 4.10.0
TypeScript: 4.8.3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  1. Visit github template
&lt;/h3&gt;

&lt;p&gt;▶︎▶︎▶︎ &lt;a href="https://github.com/matcher-inc/gas-template" rel="noopener noreferrer"&gt;&lt;strong&gt;Template for GoogleAppsScript development with TypeScript&lt;/strong&gt;&lt;/a&gt; ◀︎◀︎◀︎&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Click [Use thie template] button
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj1an2qq6hb1uvkws9cvg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj1an2qq6hb1uvkws9cvg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;※ You have to logged in to github to get the button&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Clone the repository
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone &amp;lt;Your template url&amp;gt; my-gas-project

 or

&lt;span class="c"&gt;# If you don't want to use as template&lt;/span&gt;
git clone https://github.com/matcher-inc/gas-template.git my-gas-project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Move directory
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd my-gas-project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Install npm dependencies
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Past your Script ID in the &lt;code&gt;.clasp.json&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scriptId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;Your Script ID&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"rootDir"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./dist/"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  7. If you are new to clasp, run the following command and authorize clasp
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx clasp login --no-localhost
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  8. Execute &lt;code&gt;npm run deploy&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;If &lt;code&gt;npm run deploy&lt;/code&gt; execution is successful, you will see something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm run deploy

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; gas-template@1.0.0 deploy
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; webpack &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; clasp push

asset bundle.js 3.87 KiB &lt;span class="o"&gt;[&lt;/span&gt;emitted] &lt;span class="o"&gt;(&lt;/span&gt;name: main&lt;span class="o"&gt;)&lt;/span&gt;
runtime modules 891 bytes 4 modules
cacheable modules 234 bytes
  ./src/index.ts 170 bytes &lt;span class="o"&gt;[&lt;/span&gt;built] &lt;span class="o"&gt;[&lt;/span&gt;code generated]
  ./src/sample_module/index.ts 64 bytes &lt;span class="o"&gt;[&lt;/span&gt;built] &lt;span class="o"&gt;[&lt;/span&gt;code generated]
webpack 5.74.0 compiled successfully &lt;span class="k"&gt;in &lt;/span&gt;1853 ms
└─ dist/appsscript.json
└─ dist/bundle.js
Pushed 2 files.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;--&lt;/p&gt;

&lt;p&gt;Checkpoints if it doesn't work:&lt;/p&gt;

&lt;p&gt;🚨 &lt;strong&gt;Required modules installed with &lt;code&gt;npm install&lt;/code&gt;?&lt;/strong&gt;&lt;br&gt;
If the node_modules folder is empty, &lt;code&gt;npm install&lt;/code&gt; has not been executed successfully.&lt;/p&gt;

&lt;p&gt;🚨 &lt;strong&gt;Script ID correctly pasted into .clasp.json?&lt;/strong&gt;&lt;br&gt;
If the correct Script ID is not pasted, Clasp can't reflect the latest developments.&lt;/p&gt;

&lt;p&gt;🚨 &lt;strong&gt;Clasp authorization is succeeded?&lt;/strong&gt;&lt;br&gt;
If Clasp is not authenticated, Clasp can"t reflect the latest developments.&lt;/p&gt;

&lt;p&gt;🚨 &lt;strong&gt;Running &lt;code&gt;npm run deploy&lt;/code&gt; in the correct directory?&lt;/strong&gt;&lt;br&gt;
If you have followed the steps described in the article, make sure you have run &lt;code&gt;npm run deploy&lt;/code&gt; in the my-gas-project directory.&lt;/p&gt;
&lt;h1&gt;
  
  
  ❗️ Usage notes
&lt;/h1&gt;
&lt;h3&gt;
  
  
  - Declare functions as a property of the global object at src/index.tsf
&lt;/h3&gt;

&lt;p&gt;Functions must be declared as a property of the global object as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// at src/index.ts&lt;/span&gt;

&lt;span class="c1"&gt;// import functions declared in other files&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;sampleFunc&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sample_module&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// embed an imported function in the global object&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;global&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;func1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sampleFunc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// embed an arrow function in the global object&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;global&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;func2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="na"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello hello !!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Declaring functions in this way allows functions to be selected and executed in web editor.&lt;/p&gt;

&lt;h3&gt;
  
  
  - Clasp overwrites your codes on push
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What Clasp can do is OVERRRIDE.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Clasp cannot detect differences and reflect them, so I do not recommend using this template for projects under development.&lt;/p&gt;

&lt;h1&gt;
  
  
  📚 Brief description of the project files
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs50gabn2h6t2s43wqyq7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs50gabn2h6t2s43wqyq7.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  - dist
&lt;/h3&gt;

&lt;p&gt;Webpack bundles all the files in the src folder into a single file, bundle.js. The completed file bundle.js is placed in the &lt;strong&gt;dist&lt;/strong&gt; folder.&lt;/p&gt;

&lt;p&gt;appscript.json is a kind of configuration file of GoogleAppsScript projects. There is basically no need to modify this file.&lt;/p&gt;

&lt;h3&gt;
  
  
  - node_modules
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;node_modules&lt;/strong&gt; is a folder that has modules installed by &lt;code&gt;npm install&lt;/code&gt;. What to install with &lt;code&gt;npm install&lt;/code&gt; is described in package.json, so node_modules folder is excluded from management by git. (Configured in .gitignore)&lt;/p&gt;

&lt;h3&gt;
  
  
  - src
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;src&lt;/strong&gt; is a main folder used for development. src/index.ts is a starting point for bundling files by Webpack. Please make sure to create src/index.ts. (Configured in webpack.config.js)&lt;/p&gt;

&lt;h3&gt;
  
  
  - .clasp.json
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;.clasp.json&lt;/strong&gt; is a configuration file for Clasp.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "scriptId": "&amp;lt;Your Script ID&amp;gt;",
  "rootDir": "./dist/"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Paste your Script ID into the &lt;code&gt;&amp;lt;Your Script ID&amp;gt;&lt;/code&gt; field.&lt;/p&gt;

&lt;h3&gt;
  
  
  - .gitignore
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;.gitignore&lt;/strong&gt; is a file that is used to specify what you don't want git to manage. In this template, the following three items are specified as unmanaged.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;node_modules&lt;/li&gt;
&lt;li&gt;dist/bundle.js&lt;/li&gt;
&lt;li&gt;.clasp.json&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  - package.json
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;package.json&lt;/strong&gt; is a list of packages you want installed when you &lt;code&gt;npm install&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;package-lock.json&lt;/strong&gt; and package.json are not similar.&lt;/p&gt;

&lt;p&gt;To explain very roughly&lt;br&gt;
・package.json → shopping "to do" list&lt;br&gt;
・package-lock.json → shopping "done" list&lt;br&gt;
and so on.&lt;/p&gt;

&lt;h3&gt;
  
  
  - README.md
&lt;/h3&gt;

&lt;p&gt;Just a description.&lt;/p&gt;

&lt;h3&gt;
  
  
  - tsconfig.json
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;tsconfig.json&lt;/strong&gt; is a configuration file for TypeScript. There is basically no need to modify this file.&lt;/p&gt;

&lt;h3&gt;
  
  
  - webpack.config.js
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;webpack.config.js&lt;/strong&gt; is a configuration file for Webpack. There is basically no need to modify this file.&lt;/p&gt;

&lt;h1&gt;
  
  
  🚀 Commands
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# webpack build&lt;/span&gt;
npm run build

&lt;span class="c"&gt;# webpack build in watch mode&lt;/span&gt;
npm run build:watch

&lt;span class="c"&gt;# push dist files to script.google.com&lt;/span&gt;
npm run push

&lt;span class="c"&gt;# push dist files to script.google.com in watch mode&lt;/span&gt;
npm run push:watch

&lt;span class="c"&gt;# webpack build &amp;amp; push dist files to script.google.com&lt;/span&gt;
npm run deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following two commands will make development easier:&lt;br&gt;
・&lt;code&gt;npm run build:watch&lt;/code&gt;&lt;br&gt;
・&lt;code&gt;npm run push:watch&lt;/code&gt;&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>typescript</category>
      <category>webpack</category>
      <category>googleappsscript</category>
    </item>
  </channel>
</rss>
