从 Trello 获取数据时,应用程序脚本中的 setValues() 不起作用(它们可能是 setValues() 方法中的错误?)
setValues() in apps script does not work when fetching data from Trello (could their be a bug in the setValues() method?)
我已经在下面测试了我的代码片段。它的目的是将数组的值放入 Google Sheet。有效。
`function testArray() {
var array = [["Re wire light in living room"],
["Refinish kitchen cabinets"],
["Change Sewer access cover"],
["Install cap over pvc in kitchen"],
["Install roof overhang with neighbors roof"],
["Paint side wall"],
["Touch up peeled paint in kitchen 2"],
["ท่อน้ำ"],
["Touch up paint/ repaint kitchen"],
["ต่อท่อน้ำทิ้งเครื่องซักผ้า"],
["new line"]];
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
sheet.getRange(2,1,array.length,1).setValues(array);
}`
结合使用从 Trello 获取数组值的相同代码不起作用。获取值有效。将这些值转换为数组数组是可行的。我已经记录了两者。将准备好的数组 (jobsReformatted) 放入 sheet 中不起作用。谁能告诉我出了什么问题?问题一定出在这一行:
sheet.getRange(2,1,jobsReformatted.length,1).setValues(jobsReformatted);
function fetchCardsFromTrello_() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// Prompt the user for a list name.
var listID = Browser.inputBox('Fetch Trello List ID',
'Please enter the Trello list ID',
Browser.Buttons.OK_CANCEL);
if (listID == 'cancel') {
return;
}
var base = "https://api.trello.com/1/lists/";
// var listName = "5bf10b78584430134125eb09"
var queries = "/cards?fields=id,name&";
var key = "<MY API KEY>";
var token = "<MY API TOKEN>";
var url = (base + listID + queries + "key="+key + "&" + "token="+token);
//var sheetName = "";
var trello = UrlFetchApp.fetch(url);
var json = trello.getContentText();
var data = JSON.parse(json);
Logger.log(data);
var jobs = [];
for (i = 0; i < data.length; i++) {
jobs.push(data[i].name);
}
var jobsReformatted = jobs.map(reformatJobs);
Logger.log(jobsReformatted);
sheet.getRange(2,1,jobsReformatted.length,1).setValues(jobsReformatted);
}
function reformatJobs(job) {
return "[" + job + "]";
}
我已经更新了代码,但是在 Google Sheets 中显示数组的最后一步仍然不起作用。有人可以帮忙吗?
function fetchCardsFromTrello() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// Prompt the user for a list name.
var listID = Browser.inputBox('Fetch Trello List ID',
'Please enter the Trello list ID',
Browser.Buttons.OK_CANCEL);
if (listID == 'cancel') {
return;
}
var base = "https://api.trello.com/1/lists/";
// var listName = "5bf10b78584430134125eb09"
var queries = "/cards?fields=id,name&";
var key = "<MY API KEY>";
var token = "<MY TOKEN>";
var url = (base + listID + queries + "key="+key + "&" + "token="+token);
//var sheetName = "";
var trello = UrlFetchApp.fetch(url);
var json = trello.getContentText();
var data = JSON.parse(json);
var jobs = [];
for (i = 0; i < data.length; i++) {
jobs.push(data[i].name);
}
var convertedJobs = jobs.map(convertTrelloToSheetsArray);
Logger.log(convertedJobs);
return sheet.getRange(2, 1, 10, 1).setValues(convertedJobs);
}
function convertTrelloToSheetsArray(job) {
return "[" + job + "]";
}
Logger.log(转换后的工作);生成此日志:
[18-12-22 13:49:03:059 ICT] [[重新给客厅的灯布线]、[重新装修厨柜]、[更换下水道通道盖]、[在厨房安装 PVC 盖]、 [安装与邻居屋顶的屋顶悬垂],[油漆侧墙],[修补厨房 2 中剥落的油漆],[ท่อน้ำ],[修补油漆/重新油漆厨房],[ต่อท่อน้ำทิ้งเครื้องซ]ซ]ั =13=]
- 您想在 "A2:A11" 的问题中输入 10 个值。
如果我的理解是正确的,这个修改怎么样?
修改点:
- 在您的脚本中,
convertedJobs
的每个元素都变成了字符串。
- 请将每个元素放入一维数组中。
修改后的脚本:
请修改如下
从:
var jobs = [];
for (i = 0; i < data.length; i++) {
jobs.push(data[i].name);
}
var convertedJobs = jobs.map(convertTrelloToSheetsArray);
Logger.log(convertedJobs);
return sheet.getRange(2, 1, 10, 1).setValues(convertedJobs);
到:
var jobs = [];
for (i = 0; i < data.length; i++) {
jobs.push([data[i].name]);
}
return sheet.getRange(2, 1, 10, 1).setValues(jobs);
或者,您也可以使用 map()
如下。
var jobs = data.map(function(e){return [e.name]});
return sheet.getRange(2, 1, 10, 1).setValues(jobs);
参考:
如果这不是您想要的结果,请告诉我。我想修改一下。
我已经在下面测试了我的代码片段。它的目的是将数组的值放入 Google Sheet。有效。
`function testArray() {
var array = [["Re wire light in living room"],
["Refinish kitchen cabinets"],
["Change Sewer access cover"],
["Install cap over pvc in kitchen"],
["Install roof overhang with neighbors roof"],
["Paint side wall"],
["Touch up peeled paint in kitchen 2"],
["ท่อน้ำ"],
["Touch up paint/ repaint kitchen"],
["ต่อท่อน้ำทิ้งเครื่องซักผ้า"],
["new line"]];
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
sheet.getRange(2,1,array.length,1).setValues(array);
}`
结合使用从 Trello 获取数组值的相同代码不起作用。获取值有效。将这些值转换为数组数组是可行的。我已经记录了两者。将准备好的数组 (jobsReformatted) 放入 sheet 中不起作用。谁能告诉我出了什么问题?问题一定出在这一行: sheet.getRange(2,1,jobsReformatted.length,1).setValues(jobsReformatted);
function fetchCardsFromTrello_() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// Prompt the user for a list name.
var listID = Browser.inputBox('Fetch Trello List ID',
'Please enter the Trello list ID',
Browser.Buttons.OK_CANCEL);
if (listID == 'cancel') {
return;
}
var base = "https://api.trello.com/1/lists/";
// var listName = "5bf10b78584430134125eb09"
var queries = "/cards?fields=id,name&";
var key = "<MY API KEY>";
var token = "<MY API TOKEN>";
var url = (base + listID + queries + "key="+key + "&" + "token="+token);
//var sheetName = "";
var trello = UrlFetchApp.fetch(url);
var json = trello.getContentText();
var data = JSON.parse(json);
Logger.log(data);
var jobs = [];
for (i = 0; i < data.length; i++) {
jobs.push(data[i].name);
}
var jobsReformatted = jobs.map(reformatJobs);
Logger.log(jobsReformatted);
sheet.getRange(2,1,jobsReformatted.length,1).setValues(jobsReformatted);
}
function reformatJobs(job) {
return "[" + job + "]";
}
我已经更新了代码,但是在 Google Sheets 中显示数组的最后一步仍然不起作用。有人可以帮忙吗?
function fetchCardsFromTrello() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// Prompt the user for a list name.
var listID = Browser.inputBox('Fetch Trello List ID',
'Please enter the Trello list ID',
Browser.Buttons.OK_CANCEL);
if (listID == 'cancel') {
return;
}
var base = "https://api.trello.com/1/lists/";
// var listName = "5bf10b78584430134125eb09"
var queries = "/cards?fields=id,name&";
var key = "<MY API KEY>";
var token = "<MY TOKEN>";
var url = (base + listID + queries + "key="+key + "&" + "token="+token);
//var sheetName = "";
var trello = UrlFetchApp.fetch(url);
var json = trello.getContentText();
var data = JSON.parse(json);
var jobs = [];
for (i = 0; i < data.length; i++) {
jobs.push(data[i].name);
}
var convertedJobs = jobs.map(convertTrelloToSheetsArray);
Logger.log(convertedJobs);
return sheet.getRange(2, 1, 10, 1).setValues(convertedJobs);
}
function convertTrelloToSheetsArray(job) {
return "[" + job + "]";
}
Logger.log(转换后的工作);生成此日志:
[18-12-22 13:49:03:059 ICT] [[重新给客厅的灯布线]、[重新装修厨柜]、[更换下水道通道盖]、[在厨房安装 PVC 盖]、 [安装与邻居屋顶的屋顶悬垂],[油漆侧墙],[修补厨房 2 中剥落的油漆],[ท่อน้ำ],[修补油漆/重新油漆厨房],[ต่อท่อน้ำทิ้งเครื้องซ]ซ]ั =13=]
- 您想在 "A2:A11" 的问题中输入 10 个值。
如果我的理解是正确的,这个修改怎么样?
修改点:
- 在您的脚本中,
convertedJobs
的每个元素都变成了字符串。- 请将每个元素放入一维数组中。
修改后的脚本:
请修改如下
从:var jobs = [];
for (i = 0; i < data.length; i++) {
jobs.push(data[i].name);
}
var convertedJobs = jobs.map(convertTrelloToSheetsArray);
Logger.log(convertedJobs);
return sheet.getRange(2, 1, 10, 1).setValues(convertedJobs);
到:
var jobs = [];
for (i = 0; i < data.length; i++) {
jobs.push([data[i].name]);
}
return sheet.getRange(2, 1, 10, 1).setValues(jobs);
或者,您也可以使用 map()
如下。
var jobs = data.map(function(e){return [e.name]});
return sheet.getRange(2, 1, 10, 1).setValues(jobs);
参考:
如果这不是您想要的结果,请告诉我。我想修改一下。