自定义函数输出行而不是列

时间:2018-02-24 20:43:02

标签: google-apps-script google-sheets custom-function

以下脚本从行中返回for循环中的值。反正有没有将结果返回到相邻的列中?

function readMessage(msgId){
  var url = "https://api.pipedrive.com/v1/mailbox/mailThreads/";
  var token = "/mailMessages?api_token=token";
  Logger.log("readMessage called with msgId: " + msgId);
  if (msgId){ // make sure there is a msgId in the cell 
    var rows = [] 
    var response = UrlFetchApp.fetch(url+msgId+token); 
    var dataAll = JSON.parse(response.getContentText()); 
    var dataSet = dataAll;

    var rows = [], data;

    for (var i = 0; i < dataSet.data.length; i++) {
      data = dataSet.data[i];

      rows.push([data.body_url]);
    } 

    Logger.log( JSON.stringify(rows,null,2) ); 

    return rows;
  }

当前输出是 -

   A                  B    C    D
1 =readMessage(msgId)
2 "blah blah"
3 "blah blah"

我想要的是 -

   A                       B            C          D
1 =readMessage(msgId) "blah blah"  "blah blah"
2 
3 

2 个答案:

答案 0 :(得分:0)

问题是您要返回的数据的格式。请尝试使用此rows.push(data.body_url);

for (var i = 0; i < dataSet.data.length; i++) {
    data = dataSet.data[i];

    rows.push(data.body_url); //Brackets [] have been removed to insert data as a row.
} 

如果您返回这样的数据:[[1],[3],[6]],结果将是一列值:

1

3

6

如果您的数据具有以下格式:[2,4,8],您会得到一行值:

2 | 4 | 6

来源:Custom Function

答案 1 :(得分:0)

Google Apps脚本数据始终采用2D阵列的形式。有可视化是有帮助的。

    A   B   C
1 [[A1, B1, C1],
2 [A2, B2, C2]]  

为了返回一行数据,函数应返回

[[A,B,C]] // 3 columns of data in one row

为了返回一列数据,您可以使用完整的2D数组或单个数组

[[1],[2],[3]]
OR
[1,2,3]