从Trello提取数据时,app脚本中的setValues()不起作用(它们可能是setValues()方法中的错误吗?)

时间:2018-12-22 03:18:20

标签: arrays google-apps-script google-sheets trello

我已经在下面测试了我的代码段代码。其目的是将数组的值放入Google表格中。可以。

   `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.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表格中显示数组的最后一步仍然不起作用。有人可以帮忙吗?

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(convertedJobs);产生此日志:

[18-12-22 13:49:03:059 ICT] [[重新使用客厅的电线],[整洁的厨房橱柜],[更换下水道检修盖],[在厨房的PVC上安装盖子], [与邻居屋顶一起安装屋顶悬挑],[油漆侧壁],[对厨房2中去皮的油漆进行补漆],[ท่อน้ำ],[对油漆/粉刷厨房进行补漆],[ต่อท่อน้ำทิ้งเครื่องซักผ้า]] < / p>

1 个答案:

答案 0 :(得分:0)

  • 您要在问题中将10个值添加到“ A2:A11”。

如果我的理解是正确的,那么该修改如何?

修改点:

  • 在脚本中,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);

参考:

如果这不是您想要的结果,请告诉我。我想修改它。