javascript - 使用 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))
}
}
解决方案
推荐阅读
- java - 如何在 Java 中订阅新的套接字消息
- symfony - Symfony,如何将字段添加到不在表单上的数据库
- c# - 如何将答案选择保存到访问数据库c#
- vector - 如何在迭代期间改变向量的项目?
- java-io - 如果我们将资源作为参数传递,我们可以对资源使用 try 吗,是否会在调用的方法完成执行之前关闭流
- xml - Play Xml 解析器不处理 BOM(字节排序标记)
- php - 如何打印或回显数组的值
- c++ - 哪种数据结构最适合可扩展的用户数据库
- python - 替代 seaborn pairplot 来可视化数据框?
- laravel - 出于安全原因,已禁用消息“mail() 引发的 ErrorException