はじめに
Google Apps Script で作成したプログラムを、他のユーザに配付して利用して貰いたいと考えていました。
この記事を見つけました。
[Google Apps Script API を使ってスクリプトを配布する – Wataru Nagasawa – Medium]
(https://medium.com/@nagasawa/google-apps-script-%E3%81%A7%E4%BD%9C%E3%81%A3%E3%81%9F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%82%92-apps-script-api-%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E9%85%8D%E5%B8%83%E3%81%99%E3%82%8B-aa6daf6ae642)
配付したいプログラムを準備する
プログラムを Google Apps Script で作成する
まず、Google Apps Script でプログラムを作成します。
Google ドライブにプロジェクトが作成されます。
参考 [Google Apps Scriptにおける「プロジェクト」の関係 - Qiita]
(https://qiita.com/sebisawa/items/4faa6d859f480e306f46)
作成したプログラムをダウンロードしておく
作成したプログラムを含むプロジェクトは JSON ファイルでダウンロードできます。
参考 [Google Apps Scriptのプロジェクトのコードを外部からダウンロード/アップロード(ダウンロード編) - Qiita]
(https://qiita.com/soundTricker/items/8873f29781d1e123cfa8)
Google アカウント認証の準備する
- Google アカウントを作成する。既にあればそれを使用する (https://www.google.com/accounts)
- プロジェクトを作成する (https://console.developers.google.com/project)
- 使用したい API を選択して有効にする (https://console.developers.google.com/apis/library)
- 認証情報を作成する (https://console.developers.google.com/apis/credentials)
利用したい API を有効にする
今回は Apps Script API
を有効にしておきます。
スコープを調べておく
今回は https://www.googleapis.com/auth/script.projects
https://www.googleapis.com/auth/script.deployments
になります。
ウェブクライアントアプリを作成する
参考 [ウェブアプリで Google アカウント認証して API 呼出してみた - Qiita]
(https://qiita.com/tinymouse/items/5a471cfc4a7cb05c884c)
<script type="text/javascript" src="https://apis.google.com/js/api.js"></script>
<button id="install">Install</button>
var apiKey = '◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆';
var clientId = '●●●●●●-●●●●●●●●●●●●●●●●.apps.googleusercontent.com';
var scope = 'https://www.googleapis.com/auth/drive';
$(document).ready(function(){
gapi.load('client', function(){
gapi.client.init({
apiKey: apiKey,
clientId: clientId,
scope: scope
})
.then(function(){
console.log("gapi loaded.");
})
});
});
$('#install').click(function(){
// ◎サインインする
gapi.auth2.getAuthInstance().signIn()
.then(function(obj){
// 以下続く
配付するプログラムを取得する
事前にダウンロードしておいた JSON ファイルを取得します。
function getJSON(url) {
return new Promise(function(resolve, reject){
$.ajax({
url: url,
type: 'get',
dataType: 'json',
xhrFields: {
withCredentials: true
},
success: function(result, textStatus, xhr) {
resolve(xhr.responseJSON);
},
error: function(xhr, textStatus, error) {
reject(new Error(xhr.statusText));
}
});
});
}
$('#install').click(function(){
var json, id;
// ◎サインインする
gapi.auth2.getAuthInstance().signIn()
.then(function(obj){
// ◎配付するプログラムを取得する
return getJSON("./proj.json");
})
.then(function(data){
json = data;
// 以下続く
プロジェクトを作成する
.then(function(data){
json = data;
// ①プロジェクトを作成する
return gapi.client.request({
path: 'https://script.googleapis.com/v1/projects',
method: 'POST',
body: {
title: '●●●●●●●●'
}
});
})
.then(function(res){
id = res.result.scriptId;
// 以下続く
プロジェクトを JSON ファイルの内容で更新する
.then(function(res){
id = res.result.scriptId;
// ②プロジェクトの内容を更新する
return gapi.client.request({
path: 'https://script.googleapis.com/v1/projects/' + id + '/content',
method: 'PUT',
body: json
});
})
.then(function(res){
// 以下続く
公開バージョンを作成する
.then(function(res){
// ③公開バージョンを作成する
return gapi.client.request({
path: 'https://script.googleapis.com/v1/projects/' + id + '/versions',
method: 'POST',
body: {
versionNumber: 1
}
});
})
.then(function(res){
// 以下続く
デプロイする
.then(function(res){
// ④デプロイする
return gapi.client.request({
path: 'https://script.googleapis.com/v1/projects/' + id + '/deployments',
method: 'POST',
body: {
versionNumber: 1
}
});
})
.then(function(response){
console.log('Succeeded. ');
console.log(response);
},function(reason){
console.log('Failed. ');
console.log(reason.body)
});
ウェブクライアントアプリを実行する
上記のウェブクライアントアプリを実行すると、Google アカウントにサインインする画面が開きます。そこでサインインしたアカウントの Google ドライブに、GAS プログラムがコピーされます。
配付先のアカウントで Apps Script API を有効にしておく
上記のアプリを実行してサインインする Google アカウント、つまり配付先になるアカウントで、以下のページを開き、
[Google Apps Script Dashboard]
(https://script.google.com/home)
「設定」画面で Google Apps Script API
を「オン」にしておいて貰います。