首页 > 解决方案 > 使用 Google Apps 脚本更新 Trello Card

问题描述

我正在尝试根据使用谷歌应用程序脚本更改的特定单元格使用 Trello Rest API 更新 Trello 卡。当我在 google 工作表上更新某些内容时,它会在 Trello 中使用卡片的更新信息创建另一个副本。应该更新的卡没有。

奇怪的是,有时它会起作用。我看到它工作的时候是当我将该卡移动到同一个板上的不同列表时。它有时也会创建一个副本。

我已经在本文底部发布了我的谷歌应用程序脚本代码。欢迎任何帮助!

const ui = SpreadsheetApp.getUi();
const boardId = 'xxxxxxxxxxxxxxxxxx';
const key = 'xxxxxxxxxxxxxxxxxx';
const token = 'xxxxxxxxxxxxxxxxxx';

const atEdit = (e) => {
  const {range: {getRow, getColumn}} = e;
  const [row, column] = [getRow(), getColumn()];

  const {getRange} = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
  //Syntax for a single element is .getRange(row, column)
  const [affectedCell, getRangeCells] = [getRange(row, column).getValues(), getRange(row, 1, 1, 23)];
  const googleSheetsRowId = getRangeCells.getValues()[0][0];

  getCard(googleSheetsRowId, affectedCell);
}

const getCard = (rowId, getAffectedCell) => {
  const boardUrl = 'https://api.trello.com/1/boards/' + boardId + '/cards?key=' + key + '&token=' + token;
  const response = UrlFetchApp.fetch(boardUrl);
  
  const trelloCards = JSON.parse(response);
  const {id: trelloCardId} = trelloCards[rowId];
  ui.alert(Object.keys(trelloCards[rowId]));
  updateCard(trelloCardId, getAffectedCell);
}

const buildURL = (params) => {
  var esc = encodeURIComponent;
  var query = Object.keys(params)
    .map(key => esc(key) + '=' + esc(params[key]))
    .join('&');
  return query;
}

const updateCard = (cardId, affectedData) => {
  // For now, this only works for the name but my next task is for it to work on all the values >_<
  const data = {
    'name': affectedData[0][0],
    'desc': 'sup yo',
  };
  const options = {
    'method' : 'PUT',
    'payload' : JSON.stringify(data),
    'muteHttpExceptions': true
  };

  const updateUrl = 'https://api.trello.com/1/cards/' + cardId + '?key=' + key +'&token='+ token + '&' + buildURL(data);   

  const response = UrlFetchApp.fetch(updateUrl, options);

  ErrHTTP(response);
}

const ErrHTTP = (response) => {
  const responseCode = response.getResponseCode();
  const responseBody = response.getContentText();

  if (responseCode === 200) {
    ui.alert('You got it!');
  } else {
    ui.alert(Utilities.formatString("Request failed. Expected 200, got %d: %s", responseCode, responseBody))
  }
}

标签: javascriptrestgoogle-apps-scriptgoogle-sheetstrello

解决方案


推荐阅读