Google Apps Scripit で書いたプログラムを配付できるようにしてみた

Google Apps Scripit で書いたプログラムを配付できるようにしてみた:


はじめに

Google Apps Script で作成したプログラムを、他のユーザに配付して利用して貰いたいと考えていました。

この記事を見つけました。

Google Apps Script API を使ってスクリプトを配布する – Wataru Nagasawa – Medium


配付したいプログラムを準備する


プログラムを Google Apps Script で作成する

まず、Google Apps Script でプログラムを作成します。

Google ドライブにプロジェクトが作成されます。

参考 Google Apps Scriptにおける「プロジェクト」の関係 - Qiita


作成したプログラムをダウンロードしておく

作成したプログラムを含むプロジェクトは JSON ファイルでダウンロードできます。

参考 Google Apps Scriptのプロジェクトのコードを外部からダウンロード/アップロード(ダウンロード編) - Qiita


Google アカウント認証の準備する

  1. Google アカウントを作成する。既にあればそれを使用する (https://www.google.com/accounts)
  2. プロジェクトを作成する (https://console.developers.google.com/project)
  3. 使用したい API を選択して有効にする (https://console.developers.google.com/apis/library)
  4. 認証情報を作成する (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

index.html
<script type="text/javascript" src="https://apis.google.com/js/api.js"></script> 
 
<button id="install">Install</button> 
index.js
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 ファイルを取得します。

index.js
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; 
        // 以下続く 


プロジェクトを作成する

index.js
.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 ファイルの内容で更新する

index.js
.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){ 
        // 以下続く 


公開バージョンを作成する

index.js
.then(function(res){ 
        // ③公開バージョンを作成する 
        return gapi.client.request({ 
            path: 'https://script.googleapis.com/v1/projects/' + id + '/versions', 
            method: 'POST', 
            body: { 
                versionNumber: 1 
            } 
        }); 
    })     
    .then(function(res){ 
        // 以下続く 


デプロイする

index.js
.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

「設定」画面で Google Apps Script API を「オン」にしておいて貰います。

コメント

このブログの人気の投稿

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2020-12-01 09:41:49 RSSフィード2020-12-01 09:00 分まとめ(69件)