call function which writes array to range

倖福魔咒の 提交于 2019-12-11 17:33:17

问题


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 */

}

回答1:


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 :)



来源:https://stackoverflow.com/questions/12278721/call-function-which-writes-array-to-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!