调用将数组写入范围的函数 [英] call function which writes array to range

查看:112
本文介绍了调用将数组写入范围的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我的脚本更改为在Google文档中使用新的Google Analytics(分析)API。



我完全能够从api中检索数据这可能看起来像:
$ b

[[01,5,5],[02,0,0 ],03,2,2,[04,2,6],[05,46,73],[06 15,18],[07,7,7]]



在哪里寻找帮助如何将其写入单元格。我曾经用v2 API来做,但我在这里挣扎。 (只是不明白为什么我的旧方法不起作用)。



我想要做什么:


  1. 从单元中调用函数(随机选择)

  2. 从阵列到我的工作表中的一个范围。我调用函数的单元格应该是第一个写入数据的单元格。 be:

      function testAnalytics(){

    var id =ga:XXXXXXXX;
    var startdate =2012-01-01;
    var enddate =2012-07-31;
    var metrics =ga:visits,ga:pageviews;
    var optArgs = {dimensions:ga:month};
    var grabData = Analytics.Data.Ga.get(id,startdate,enddate,metrics,optArgs);
    // Browser.msgBox(grabData.getRows()); //测试数据是否被正确接收
    var returnVal = grabData.getRows();
    返回returnVal;

    / *将returnVal写入活动电子表格中的活动单元格* /

    }


    '的div类= h2_lin>解决方案

尝试

 变种片= SpreadsheetApp.getActiveSheet( ); 
var thisCell = SpreadsheetApp.getActiveRange();
var row = thisCell.getRow();
var col = thisCell.getColumn();
//假设二维数组中的每一行都有相同数量的元素。
sheet.getRange(row,col,returnVal.length,returnVal [0] .length).setValues(returnVal);

如果您想要写入任何其他单元格,请适当更改thisCell。
注意我没有自己测试过,所以如果出现语法错误,请随时纠正它们:)


I am trying to change my scripts to use the new google analytics API within Google Docs.

I am perfectly able to retrieve my data as an array from the api which might look like:

[["01", "5", "5"], ["02", "0", "0"], ["03", "2", "2"], ["04", "2", "6"], ["05", "46", "73"], ["06", "15", "18"], ["07", "7", "7"]]

Where I am looking for some help is on how to write this to a cell. I used to do so with the v2 API, but am struggling here. (just do not understand why my old method is not working).

What I would like to do:

  1. Call a the function from a cell (randomly chosen)

  2. drop the results from the array to a range in my sheet. The cell where I call the function should be the first cell to write the data.

The beginning of the function would be:

function testAnalytics() {

      var id = "ga:XXXXXXXX";
      var startdate = "2012-01-01";
      var enddate = "2012-07-31";
  var metrics = "ga:visits, ga:pageviews";
  var optArgs = {dimensions: "ga:month"};
  var grabData = Analytics.Data.Ga.get(id,startdate, enddate,metrics,optArgs); 
//  Browser.msgBox(grabData.getRows()); // test to see if data is correctly received 
  var returnVal = grabData.getRows();
  return returnVal; 

/* write returnVal to active cell on active spreadsheet */

}

解决方案

Try

var sheet = SpreadsheetApp.getActiveSheet(); 
var thisCell = SpreadsheetApp.getActiveRange(); 
var row = thisCell.getRow();
var col = thisCell.getColumn(); 
//Assuming every row in the 2D array has the same number of elements. 
sheet.getRange(row, col, returnVal.length , returnVal[0].length).setValues(returnVal); 

If you want it to write to any other cell, change thisCell appropriately. Note I haven't tested this myself so if there are syntax errors, please feel free to correct them :)

这篇关于调用将数组写入范围的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆