LoginSignup
9
11

More than 5 years have passed since last update.

GAS Turorialsのススメ

Last updated at Posted at 2012-12-08

これはGoogle Apps Script Advent Calendar 2012の8日目の記事です。
今日はハードルを下げまくって、HelloWorldな話です。

GASの公式Tutorialsのページは充実しているので、お時間があるときに眺めてみるといいですよ、という話です。
「そのページ見たことある!」→ 頻繁に足を運んで見ましょう。ほら「Last updated December 7, 2012.」って下に書いてある。昨日じゃないですか。
「でも英語でしょ?」→ 案外読めます。英語がわかった気になるのでお得です。
「やっぱり日本語のほうが・・」→ 親切な人が翻訳してくれたサイトもあるのですが、Tutorialsの更新が意外と頻繁に行われているので追いついていないようです。もちろん日本語で予習してから本家を見に行くと理解が早いですし、ありがたく利用させていただいております。

早速わかった気分になる?Tutorialsテキトー翻訳を始めます。

Tutorials

https://developers.google.com/apps-script/articles

Basics and Custom Spreadsheet Functions

Your First Script - This tutorial covers the basics of writing and executing a script, demonstrating how to create a Google Document and send an email.

一番上にある「Your First Script」を見てみましょう。
一番上にあるので、多分これがHelloWorldです。
説明文もついてますね。
「how to create a Google Document and send an email.」ドキュメントを作って、メールも送れるようです。


では早速リンク先へ。

Building Your First Script

「あ、なんか違うかも・・」
ひるんではいけません。「Building」が先頭についただけです。

Requirements

読み飛ばしても多分大丈夫です。Googleのアカウント持ってる?とかそんな話です。
Google Apps Script Advent Calendar一日目の5分で始めるGoogle Apps Scriptを読むといいと思います。

What is a Script?

このAPI使うよ!とか書いてあります。
あとで見ればいいです。

Writing a Script

ここからが大事なところです。

・From Google Drive, choose Create > More > Script.

Googleドライブ→作成→もっと見る→スクリプト
と書いてあります。それくらい私にも読めます。空のプロジェクトを作ればいいみたいです(意訳)

The script you will create is a very simple one. It will create a new Google Document and email you a link to the newly created document. Delete the placeholder code in your new script, and then copy and paste the code below into the Script Editor.

デフォルトのmyFunction()を消して、下のcreateAndSendDocument()のコードをコピペしろ、と書いてあります。そうします。

コードは↓です。

コード.gs
function createAndSendDocument() {
  // Create a new document with the title 'Hello World'
  var doc = DocumentApp.create('Hello World');

  // Add a paragraph to the document
  doc.appendParagraph('This document was created by my first Google Apps Script.');

  // Save and close the document
  doc.saveAndClose();

  // Get the URL of the document
  var url = doc.getUrl();

  // Get the email address of the active user - that's you
  var emailAddress = Session.getActiveUser().getEmail();

  // Send yourself an email with a link to the document
  GmailApp.sendEmail(emailAddress,
                     'Hello from my first Google Apps Script!',
                     'Here is a link to a document created by my ' +
                     'first Google Apps Script: ' + url);
}

After you paste the code into the Script Editor, click the Save icon. You'll be prompted to rename your project. Enter the name My First Script and then click OK. Now that you've created the script, move on to the next section to learn how to execute the script.

コピペしたら名前を変えて保存しろと言ってます。「無題のプロジェクト」より「My First Script」(どっちでもいいけど)

Executing a Script

いよいよ実行します。

To execute the script, either click the Run icon or choose Run > createAndSendDocument from the menu.

▶アイコンか、メニュー→実行で実行できるよ。

You will see an authorization dialog appear.

その後、承認ダイアログがでるよ。

auth

This dialog tells you which services the script needs to access. Click Authorize. Next, you'll see another dialog, prompting you to grant access to your Gmail account. This is needed so that the script can send email from your Gmail account. Click Grant access.

緑の枠で囲ったやつ、でてきましたか?
OK(Authorize)しましたか?
まだダイアログ続くよ。
GmailにGrantしてね。

oauth

Now you'll see an Authorization Status page, which tells you that you can now run the script. Click Close. Note that you will only have to go through this process the first time you run a script or when a script has been modified to require more permissions than you'd previously granted.

grant系のメッセージが出てくるのは初回だけだよ。

Now that the script has been authorized, run it once more. This time, the script will execute. Next go to your Gmail inbox and you should see an email from your script that looks similar to this.

「run it once more」

はい、重要です。あれ?実行したけどメール来てないし・・となった人、

もう一度実行してください。

・・自分からメール来ましたか?
・・ドキュメントは作成されましたか?

おめでとうございます!(ぱちぱちぱち)

こんな感じでコピペで実行してみると楽しいです。
英語は雰囲気で。

ソースをコピペしてみたら、動かないんだけど!
という場合は、MLとかで聞いてみるとか検索しまくるといいと思います。

あと、翻訳がまちがってたらごめんなさい。

9
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
11