Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
GAS ビギナーが GAS を使いこな すために知るべきこと 10 選 / for Googl...
Search
tanabee
April 03, 2018
Programming
5.8k
6
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
GAS ビギナーが GAS を使いこな すために知るべきこと 10 選 / for Google App Script begginers
Qiita:
https://qiita.com/tanabee/items/2c51681396fe12b6a0e4
tanabee
April 03, 2018
More Decks by tanabee
See All by tanabee
React x Firebase を用いた Web アプリケーション開発時のハマりどころと解決策 / React x Firebase tips
tanabee
6
1.2k
Google Apps Script で始める RPA / RPA starting with GAS
tanabee
1
52k
Other Decks in Programming
See All in Programming
Vue Fes Japan の “作る側”に関わってみて
naokihaba
0
140
AWSに止められる覚悟してますか?
morizo_1984
2
520
Simple Storage Service(S3) is not simple
iwatsukayura
0
230
APNsからLive Activityを開始する話
yumnumm
0
160
GemmaをJevのように使ってみる / Use Gemma like Jev
kishida
5
740
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
21
21k
そのリトライ、死んだコネクションを使い回していませんか ── GoのHTTPクライアントとHTTP/2を実プロダクト障害から学び直す
myus4a
1
410
Atomic Design, Enforced: Scaling Mobile Design Systems (next.app devCon / droidCon Berlin 2026)
steliosf
PRO
0
160
技術的負債の返済は、AI時代の複利で効く投資 — 経営としての意思決定とその遂行
curekoshimizu
1
2.3k
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Rails World 2026, Austin, Texas
marcoroth
0
190
Ghostty + Neovimで作る 透明でカッコ良い開発環境
j341nono
0
160
見えないものを探る要求要件定義に必要な基本的思考 / invisible-requirement-thinking
minodriven
15
8k
Featured
See All Featured
GraphQLとの向き合い方2022年版
quramy
50
15k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
740
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k
Side Projects
sachag
456
43k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Technical Leadership for Architectural Decision Making
baasie
3
600
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.2k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
480
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
350
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
The Cost Of JavaScript in 2023
addyosmani
55
10k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.9k
Transcript
GAS ビギナーが GAS を使いこな すために知るべきこと 10 選 株式会社グロービス Yuki Tanabe
/urls - Speaker Deck : bit.ly/2pXozRi - Qiita : bit.ly/2GsvMUy
/me 株式会社グロービス リードエンジニア - github / Qiita : tanabee - twitter:
@_tanabee プライベートで iOS / Android アプリ開発 累計 100 万 DL 達成
/me - Qiita に GAS ネタ投稿してます - Google Apps Script
(GAS) で毎週 30 分の雑務を自動化した話 - 3 分で作る無料の翻訳 API with Google Apps Script - Qiita Organizations Ranking
/about - GAS の便利な使い方 - GAS で効率良く開発する方法 - GAS の詰まりどころ
- などなど
1. ローカル環境で開発する 公式の npm パッケージ利用 https://github.com/google/clasp $ clasp clone <fieldId>
$ clasp push $ clasp pull
2. github でソースコード管理する - ローカルで開発して git 連携 - Chrome 拡張:
Google Apps Script Github アシスタント ↑この辺が拡張されて github に push / pull できるようになる
3. 複数ファイル構成 - 複数の *.gs ファイルを持てる - 定義された関数はグローバル関数となる
function main() { var num = add(1, 2); Logger.log(num); }
function add(a, b) { return a + b; } main.gs util.gs
4. 様々な実行方法 主な実行方法 - トリガー - G Suite サービスのイベントで発火 -
Web 公開
4. トリガー - “毎週何曜日のこの時間帯に実行” など - 分 / 時 /
日 / 週 / 月タイマー - Heroku でいう Scheduler - 編集 > 現在のプロジェクトのトリガー
- 特定の Docs を開いた時 - スプレッドシート更新時 - Google フォーム回答時 -
などなど 公式ガイドの Event Objects 参照 https://developers.google.com/apps-script/guides/triggers/events 4. G Suite サービスのイベントで発火
4. Web 公開 - スクリプトを API や Web ページとして公開できる -
公開 > ウェブアプリケーションとして導入
4. ミニマムな API 実装 function doGet(e) { return ContentService.createTextOutput("hello world!");
} function doPost(e) { return ContentService.createTextOutput("OK"); }
function doGet(e) { return ContentService.createTextOutput("hello world!"); } function doPost(e) {
return ContentService.createTextOutput("OK"); } doGet と doPost を実装 - doGet: GET リクエストに反応 - doPost: POST リクエストに反応 4. ミニマムな API 実装
function doGet(e) { return ContentService.createTextOutput("hello world!"); } function doPost(e) {
return ContentService.createTextOutput("OK"); } 以下のいずれかの型の値を返す - HtmlOutput - TextOutput https://developers.google.com/apps-script/guides/web 4. ミニマムな API 実装
function doGet(e) { return ContentService.createTextOutput("hello world!"); } function doPost(e) {
return ContentService.createTextOutput("OK"); } e: Event Object リクエスト時の値を参照できる - doGet: e.queryString, e.parameter - doPost: e.postData.contents https://developers.google.com/apps-script/guides/web 4. ミニマムな API 実装
5. スクリプト毎のデータストア 前回の実行結果など、スクリプト毎に簡単なデータを保存する保存領域が用意されている var properties = PropertiesService.getScriptProperties(); properties.getProperty(key);// 取得 properties.setProperty(key,
value);// 登録 properties.deleteProperty(key);// 削除
6. スクリプトからもトリガーを設定できる TriggerBuilder を利用 https://developers.google.com/apps-script/reference/script/trigger-builder var func = 'main'; var
d = new Date(); d.setMinutes(dt.getMinutes() + 1);// 1 分後 ScriptApp.newTrigger(func).timeBased().at(dt).create();
7. 実行時間の制限 - 1 回の実行で 6 分間の時間制限 - スクリプトの実行途中でも強制終了 https://developers.google.com/apps-script/guides/services/quotas
7. 6 分を超える処理を実行する方法 例)スプレッドシートの各行処理するスクリプト 1. 実行時間が 5 分になるのを検知 2. 次回処理を開始する行数を
PropertiesService で保存 3. TriggerBuilder を使って新しいトリガーを 1 分後に設定 4. 処理を終了させる ※ 5 分や 1 分というのは仮の値なので、正常に動作する適切な時間を選んでください。
8. G Suite 以外の Web API をたたく function postSlack(slackWebhookUrl) {
var body = { text: 'message' }; var payload = JSON.stringify(body); var res = UrlFetchApp.fetch(slackWebhookUrl, { method: 'POST', headers: { "Content-Type": 'application/json' }, payload: payload }); } UrlFetchApp.fetch で API リクエスト(POST Slack)
9. Web スクレイピングの方法 1. UrlFetchApp.fetch で html を取得 2. html
を parse a. GAS 公式のサンプルを利用 b. 独自の抽出ロジックを実装
9. 公式サンプルを利用した parse 以下のメソッドを別途定義する - getElementById() - getElementsByClassName - getElementsByTagName
https://sites.google.com/site/scriptsexamples/learn-by-example/parsing-html
9. 公式サンプルを利用した parse var html = UrlFetchApp.fetch(url).getContentText(); var doc =
XmlService.parse(html); var element = doc.getRootElement(); var menu = getElementsByClassName(element, 'menu')[0]; https://sites.google.com/site/scriptsexamples/learn-by-example/parsing-html > XmlService.parse でこけた時に詰む
9. 独自の抽出ロジックを実装 var html = UrlFetchApp.fetch(url).getContentText(); var username = find(html,
'<p id="username">', '</p>'), function find(text, from, to) { var fromIndex = text.indexOf(from); if (fromIndex === -1) return ''; text = text.substring(fromIndex + from.length); var toIndex = text.indexOf(to); if (toIndex === -1) return ''; return text.substring(0, toIndex); }
10. GAS でライブラリを利用する - GAS では npm を利用できない - 代わりに独自の
GAS ライブラリという仕組みがある - 他の人が公開した GAS を参照して実行するような仕組み - が、実行速度が遅いので add-on での利用は控えるべきとのこと - 良い方法知ってる方いたら教えてくださいmm https://developers.google.com/apps-script/guides/libraries
ご清聴ありがとうございました