Trello: Create Card

Trello: Create Card

Trello: カード作成

This item creates a card on Trello.

Auto Step icon
Basic Configs
Step Name
Note
Configs for this Auto Step
conf_ApiKey
C1: Authorization Setting in which API Key is set *
conf_ApiToken
C2: Authorization Setting in which API Token is set *
conf_ListId
C3: List ID to create the card in *
conf_Name
C4: Card Title *#{EL}
conf_Desc
C5: Card Description#{EL}
conf_LabelIds
C6: Label IDs to add to the card (write one per line)
conf_MemberIds
C7: Member IDs to add to the card (write one per line)
conf_Start
C8: Start Date
conf_Due
C9-1: Due Datetime
conf_DueReminder
C9-2: Reminder Setting (only effective when C9-1 is set)
conf_CardId
C10: Data item to save ID of the card
conf_CardUrl
C11: Data item to save URL of the card detail page

Notes

  • To get your API Key and API Token,
    1. Open the Power-Ups administration page and create a new Power-Up
    2. Proceed to the “API Key” page from the left menu, and your API Key is shown on the page
    3. Click the link in “you can manually generate a Token” to the right of the API Key to get your API Token
  • IDs of the components (such as Lists, Labels and Members) are not displayed on Trello Web UI, so please use the following items to retrieve IDs:

Capture

See Also

Script (click to open)
  • An XML file that contains the code below is available to download
    • trello-card-create.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on auto step


function main() {
    //// == 工程コンフィグ・ワークフローデータの参照 / Config & Data Retrieving ==
    const auth = retrieveAuth(); // {apiKey, apiToken}
    const listId = retrieveListId();
    const name = retrieveName();
    const desc = configs.get('conf_Desc');
    const labelIds = retrieveIdsAsList('conf_LabelIds');
    const memberIds = retrieveIdsAsList('conf_MemberIds');
    const start = retrieveStart();
    const due = retrieveDue();
    const dueReminder = retrieveDueReminder();
    const cardIdDef = configs.getObject('conf_CardId');
    const cardUrlDef = configs.getObject('conf_CardUrl');

    //// == Calculating / 演算 ==
    const card = createCard(auth, listId, name, desc, labelIds, memberIds, start, due, dueReminder);

    //// == Data Updating / ワークフローデータへの代入 ==
    setData(cardIdDef, card.id);
    setData(cardUrlDef, card.url);
}

/**
  * config の認証設定から API キーと API トークンを読み出す
  * @return {Object} auth
  * @return {String} auth.apiKey
  * @return {String} auth.apiToken
  */
function retrieveAuth() {
    const authKey = configs.getObject("conf_ApiKey");
    const authToken = configs.getObject("conf_ApiToken");
    const apiKey = authKey.getToken();
    const apiToken = authToken.getToken();
    return {apiKey, apiToken};
}

/**
  * config からリスト ID を読み出す
  * @return {String} listId
  */
function retrieveListId() {
    const listIdDef = configs.getObject('conf_ListId');
    if (listIdDef === null) { // 固定値で指定。必須項目なので、空文字列はありえない
        return configs.get('conf_ListId');
    }
    // 文字型データ項目の場合
    if (listIdDef.matchDataType('STRING_TEXTFIELD')) {
        const listId = engine.findData(listIdDef);
        if (listId === null) {
            throw new Error('List ID is blank.');
        }
        return listId;
    }
    // 選択型データ項目の場合
    const selects = engine.findData(listIdDef);
    if (selects === null || selects.size() === 0) {
        throw new Error('List ID is not selected.');
    }
    return selects.get(0).getValue();
}

/**
  * config からカードのタイトルを読み出す
  * @return {String} name
  */
function retrieveName() {
    const name = configs.get('conf_Name');
    if (name === null || name === '') {
        throw new Error('Card Title is blank.');
    }
    return name;
}

/**
  * config に設定された ID 一覧を読み出す
  * @param {String} confName 設定名
  * @return {Array<String>} ids
  */
function retrieveIdsAsList(confName) {
    const dataDef = configs.getObject(confName);
    if (dataDef === null) {
        return [];
    }
    // 文字型データ項目の場合
    if (dataDef.matchDataType('STRING')) {
        const dataObj = engine.findData(dataDef);
        if (dataObj === null) {
            return [];
        }
        return dataObj.split('\n').filter(id => id !== '');
    }
    // 選択型データ項目の場合
    const selects = engine.findData(dataDef);
    if (selects === null || selects.size() === 0) {
        return [];
    }
    const ids = [];
    selects.forEach(item => {
        ids.push(item.getValue()); // 選択肢 ID を格納
    });
    return ids;
}

/**
  * config から開始日を文字列として読み出す
  * @return {String} start
  */
function retrieveStart() {
    const startDef = configs.getObject('conf_Start');
    if (startDef === null) {
        return null;
    }
    const startObj = engine.findData(startDef); // AddableDate
    if (startObj === null) {
        return null;
    }
    return startObj.toString(); // yyyy-MM-dd
}

/**
  * config から期限を文字列として読み出す
  * @return {String} due
  */
function retrieveDue() {
    const dueDef = configs.getObject('conf_Due');
    if (dueDef === null) {
        return null;
    }
    const dueObj = engine.findData(dueDef); // AddableTimestamp
    if (dueObj === null) {
        return null;
    }
    return dateFormatter.format('UTC', "yyyy-MM-dd'T'HH:mm:ss'Z'", dueObj);
}

/**
  * config からリマインダーの設定を読み出す
  * @return {Number} dueReminder
  */
function retrieveDueReminder() {
    const dueReminder = configs.get('conf_DueReminder');
    if (dueReminder === null || dueReminder === '') {
        return null;
    }
    return parseInt(dueReminder, 10);
}

/**
  * カードを作成する
  * @param {Object} auth
  * @param {String} auth.apiKey
  * @param {String} auth.apiToken
  * @param {String} listId
  * @param {String} name
  * @param {String} desc
  * @param {Array<String>} labelIds
  * @param {Array<String>} memberIds
  * @param {String} start
  * @param {String} due
  * @param {Number} dueReminder
  * @return {Object} card
  */
function createCard({apiKey, apiToken}, listId, name, desc, labelIds, memberIds, start, due, dueReminder) {
    const requestBody = {
        idList: listId,
        name,
        desc,
        idLabels: labelIds,
        idMembers: memberIds,
        start,
        due,
        dueReminder
    };
    const response = httpClient.begin()
        .body(JSON.stringify(requestBody), 'application/json')
        .post(`https://api.trello.com/1/cards?key=${apiKey}&token=${apiToken}`);
    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();
    if (status !== 200) {
        engine.log(responseStr);
        throw new Error(`Failed to create card. status: ${status}`);
    }
    return JSON.parse(responseStr);
}

/**
  * データ項目にデータを保存する
  * @param {DataDefinitionView} dataDef データ項目の DataDefinitionView
  * @param {Object} value 保存する値
  */
function setData(dataDef, value) {
    if (dataDef !== null) {
        engine.setData(dataDef, value);
    }
}

    
Scroll to Top

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading