1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AsanaとGoogle Apps Scriptを連携する

Last updated at Posted at 2021-06-02

##Google Apps ScriptからAsana APIを呼んでみる。

メールが来たら、Asanaにタスクを立てるとか、Asanaのタスクを全部読んで報告シートを更新するとか、Google Apps ScriptからAsana APIを呼びだして利用すると便利です。

今回、Asanaを使うコードをクラス化してみました。


class AsanaManager {
  constructor(token) {
    this.token = token;
  }

  //プロジェクトのタスク一覧を取得
  getAllTaskByProject(projectId) {
    const url = 'https://app.asana.com/api/1.0/projects/' + projectId + '/tasks';
    const data = this.getAsanaData(url)
    return data;
  };

  //プロジェクトのセクション一覧を取得
  getAllSectionByProject(projectId) {
    const url = 'https://app.asana.com/api/1.0/projects/' + projectId + '/sections';
    const data = this.getAsanaData(url)
    return data;
  };

  //セクションのタスク一覧を取得
  getAllTaskBySection(sectionId) {
    const url = 'https://app.asana.com/api/1.0/sections/' + sectionId + '/tasks';
    const data = this.getAsanaData(url)
    return data;
  };

  //タスクの情報を取得
  getTaskData(task_gid) {
    const url = 'https://app.asana.com/api/1.0/tasks/' + task_gid;
    const data = this.getAsanaData(url)
    return data;
  };

  //カスタムフィールドを取得
  getACustomField(customFieldId) {
    const url = 'https://app.asana.com/api/1.0/custom_fields/' + customFieldId;
    const data = this.getAsanaData(url)
    return data;
  };


  //getでAsanaからデータ取得する関数
  getAsanaData(url) {
    console.log(url);
    const token = this.token;
    const options = this.getGetOption(token);
    const response = UrlFetchApp.fetch(url, options);
    const jobj = JSON.parse(response);
    const data = jobj["data"];
    return data;
  }

  //getなオプションを作成するよ。
  getGetOption(token) {
    const headers = {
      Authorization: "Bearer " + token,
    };
    const options = {
      method: "get",
      contentType: "application/json",
      headers: headers,
      muteHttpExceptions: true,
    };
    return options;
  }

  //ここからタスク作る子たち

  //タスクの作成
  //https://developers.asana.com/docs/create-a-task
  //objPayload(object) Body parameter
  createTask(objPayload) {
    const url = `https://app.asana.com/api/1.0/tasks`;
    const data = this.postAsanaData(url, objPayload);
    console.log('createTask', data);
    return data;
  }

  //セクションにタスクを移動するよ
  //https://developers.asana.com/docs/create-a-section-in-a-project
  //sectionId(Number) 移動したいセクションID
  //objPayload(object) Body parameter
  addTaskToSection(sectionId, objPayload) {
    const url = `https://app.asana.com/api/1.0/sections/${sectionId}/addTask`;
    const data = this.postAsanaData(url, objPayload);
    console.log('addTaskToSection', data);
    return data;
  }

  //サブタスクを作成するよ
  //https://developers.asana.com/docs/create-a-subtask
  //taskId(Number)サブタスクを追加するタスクID
  //objPayload(object) Body parameter
  createSubtask(taskId, objPayload) {
    const url = `https://app.asana.com/api/1.0/tasks/${taskId}/subtasks`;
    const data = this.postAsanaData(url, objPayload);
    console.log('createSubtask', data);
    return data;
  }

  //postでAsanaからデータ追加する関数
  postAsanaData(url, objPayload) {
    console.log(url);
    const token = this.token;
    const options = this.getPostOption(token, objPayload);
    const response = UrlFetchApp.fetch(url, options);
    console.log(response.getResponseCode());
    const jobj = JSON.parse(response);
    const data = jobj["data"];
    return data;
  }

  //postバージョンのオプションを返すよ
  getPostOption(token, objPayload) {
    const headers = {
      "Authorization": "Bearer " + token
    };
    const payload = this.getJson(objPayload);
    const options = {
      "method": "post",
      "contentType": "application/json",
      "Accept": "application/json",
      "headers": headers,
      "payload": payload,
      "muteHttpExceptions": true
    }
    console.log(options);
    return options;
  }

  //オブジェクトをJSONにする
  getJson(object) {
    var json = JSON.stringify(object);
    console.log(json);
    return json;
  }

  //ここからタスクを更新する子たち(put)

  //タスクを更新するよするよ
  //https://developers.asana.com/docs/update-a-task
  //taskId(Number) 更新するタスクID
  //objPayload(object) Body parameter
  updateTask(taskId, objPayload) {
    const url = `https://app.asana.com/api/1.0/tasks/${taskId}`;
    const data = this.putAsanaData(url, objPayload);
    console.log('updateTask', data);
    return data;
  }

  //putでAsanaのデータを変更する関数
  putAsanaData(url, objPayload) {
    console.log(url);
    const token = this.token;
    const options = this.getPutOption(token, objPayload);
    const response = UrlFetchApp.fetch(url, options);
    console.log(response.getResponseCode());
    const jobj = JSON.parse(response);
    const data = jobj["data"];
    return data;
  }

  //putバージョンのオプションを返すよ
  getPutOption(token, objPayload) {
    const headers = {
      "Authorization": "Bearer " + token
    };
    const payload = this.getJson(objPayload);
    const options = {
      "method": "put",
      "contentType": "application/json",
      "Accept": "application/json",
      "headers": headers,
      "payload": payload,
      "muteHttpExceptions": true
    }
    console.log(options);
    return options;
  }


}

次は、使い方を紹介いたします。

1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?