Search code examples
google-apps-scripttrello

How to run this Ex(Trello API) code on Google App Script


I want to use Trello API to execute on google-apps-script. but google-apps-script does not support ES6 promise.

JS

var url = "https://api.trello.com/1/cards/{idCard}/customField/{idCustomField}/item?token={yourToken}&key={yourKey}";
var data = {value: { number: "42" }};
fetch(url, { body: JSON.stringify(data), method: 'PUT', headers: {'content-type': 'application/json'}})
.then((resp) => resp.json())
.then((data) => console.log(JSON.stringify(data, null, 2)))
.catch((err) => console.log(JSON.stringify(err, null, 2)))

Solution

    • You want to convert the following script to Google Apps Script.

      var url = "https://api.trello.com/1/cards/{idCard}/customField/{idCustomField}/item?token={yourToken}&key={yourKey}";
      var data = {value: { number: "42" }};
      fetch(url, { body: JSON.stringify(data), method: 'PUT', headers: {'content-type': 'application/json'}})
      .then((resp) => resp.json())
      .then((data) => console.log(JSON.stringify(data, null, 2)))
      .catch((err) => console.log(JSON.stringify(err, null, 2)))
      
    • Your script works fine.

    If my understanding is correct, how about this modification?

    Modified script:

    var url = "https://api.trello.com/1/cards/{idCard}/customField/{idCustomField}/item?token={yourToken}&key={yourKey}";
    var data = {value: { text: "42" }}; // <--- Modified
    var params = {
      method: 'PUT',
      contentType: 'application/json',
      payload: JSON.stringify(data)
    };
    var res = UrlFetchApp.fetch(url, params);
    Logger.log(res.getContentText())
    
    • Before you run the script, please modify url with your parameters.

    Note:

    • There are 6 custom field types like "Text", "Number", "Date", "Checkbox" and "List".
    • If you use the text field for the custom field, you can use "Text" as the Custom Field Type. It's like { "value": { "text": "Hello, world!" } }.
    • If you use the number field for the custom field, you can use "Number" as the Custom Field Type. It's like { "value": { "number": "42" } }.

    References:

    If this didn't work, I apologize.