Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 将面板数据复制到Google工作表_Loops_Google Apps Script_Google Sheets_Mixpanel - Fatal编程技术网

Loops 将面板数据复制到Google工作表

Loops 将面板数据复制到Google工作表,loops,google-apps-script,google-sheets,mixpanel,Loops,Google Apps Script,Google Sheets,Mixpanel,我正在尝试使用以下代码将mixpanel数据推送到单个Google工作表中: 我对API\u参数没有按预期循环出现问题。(公式、公式1和公式2)前两个参数循环通过fine,但当添加最终参数时,我得到错误: TypeError:无法从未定义中读取属性“2016-07-11”。(第143行,文件“代码”) 代码如下: * Step 1) Fill in your account's Mixpanel Information here `enter code here`*/ var API_KEY

我正在尝试使用以下代码将mixpanel数据推送到单个Google工作表中:

我对
API\u参数
没有按预期循环出现问题。(公式、公式1和公式2)前两个参数循环通过fine,但当添加最终参数时,我得到错误:

TypeError:无法从未定义中读取属性“2016-07-11”。(第143行,文件“代码”)

代码如下:

* Step 1) Fill in your account's Mixpanel Information here
 `enter code here`*/
var API_KEY = '******';
var API_SECRET = '****';

/**
 * Step 2) Define the tab # at which to create new sheets in the spreadsheet.
 * 0 creates a new sheet as the first sheet. 
 * 1 creates a new sheet at the second sheet and so forth.
 */
var CREATE_NEW_SHEETS_AT = 0;

/**
 * Step 3) Define date range as a string in format of 'yyyy-mm-dd' or '2013-09-13'
 *
 * Today's Date: set equal to getMixpanelDateToday() 
 * Yesterday's Date: set equal to getMixpanelDateYesterday() 
 */
var FROM_DATE = getMixpanelDate(7);
var TO_DATE = getMixpanelDate(1);

/**
 * Step 4) Define Segmentation Queries - Get data for an event, segmented and filtered by properties.

var API_PARAMETERS = {
   'Formula' : [ 'QuestionAsked', '(properties["InputMethod"]) == "Voice" or (properties["InputMethod"]) == "Text" ', 'general', 'day', 'B7'],
   'Formula1' : [ 'QuestionAsked', '(properties["InputMethod"]) == "Voice" or (properties["InputMethod"]) == "Text" ', 'unique', 'day', 'B2'],
   //'Formula2' :  [ 'QuestionAnswered', '(properties["InputMethod"]) == "Voice" or (properties["InputMethod"]) == "Text" ', 'unique', 'day', 'B3' ],
};





// Iterates through the hash map of queries, gets the data, writes it to spreadsheet
function getMixpanelData() {
  for (var i in API_PARAMETERS)
  {
    var cell = API_PARAMETERS[i][4];
    fetchMixpanelData(i, cell);

  }
}

// Creates a menu in spreadsheet for easy user access to above function
function onOpen() {
  var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  activeSpreadsheet.addMenu(
      "Mixpanel", [{
        name: "Get Mixpanel Data", functionName: "getMixpanelData"
      }]);
}

/**
 * Gets data from mixpanel api and inserts to spreadsheet
 *
 */
function fetchMixpanelData(sheetName, cell) {

  var c = cell;

  var expires = getApiExpirationTime();
  var urlParams = getApiParameters(expires, sheetName).join('&')
       + "&sig=" + getApiSignature(expires, sheetName);

  // Add URL Encoding for special characters which might generate 'Invalid argument' errors. 
  // Modulus should always be encoded first due to the % sign.
  urlParams = urlParams.replace(/\%/g, '%25');   
  urlParams = urlParams.replace(/\s/g, '%20');
  urlParams = urlParams.replace(/\[/g, '%5B');
  urlParams = urlParams.replace(/\]/g, '%5D');
  urlParams = urlParams.replace(/\"/g, '%22');
  urlParams = urlParams.replace(/\(/g, '%28');
  urlParams = urlParams.replace(/\)/g, '%29');
  urlParams = urlParams.replace(/\>/g, '%3E');
  urlParams = urlParams.replace(/\</g, '%3C');
  urlParams = urlParams.replace(/\-/g, '%2D');   
  urlParams = urlParams.replace(/\+/g, '%2B');   
  urlParams = urlParams.replace(/\//g, '%2F');

  var url = "http://mixpanel.com/api/2.0/segmentation?" + urlParams;
  Logger.log("THE URL  " + url);
  var response = UrlFetchApp.fetch(url);

  var json = response.getContentText();
  var dataAll = JSON.parse(json);

  var dates = dataAll.data.series;
  Logger.log(API_PARAMETERS);
  for (i in API_PARAMETERS){
    var parametersEntry = API_PARAMETERS[i]; 
    for (i in dates){
      data = dataAll.data.values[parametersEntry[0]][dates[i]]; 
    }
    insertSheet(data, c);
  };
}


function insertSheet(value, cell) {

  var sheetName = 'Formula';
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(sheetName);

  var c = sheet.getRange(cell).setValue(value);


};



/** 
 * Returns an array of query parameters
 */
function getApiParameters(expires, sheetName) {
  var parametersEntry = API_PARAMETERS[sheetName];
  return [
        'api_key=' + API_KEY,
        'expire=' + expires,
        'event=' + parametersEntry[0],
        'where=' + parametersEntry[1],
        'type=' + parametersEntry[2],
        'unit=' + parametersEntry[3],
        'from_date=' + FROM_DATE,
        'to_date=' + TO_DATE
    ];
}

/** 
 * Sorts provided array of parameters
 *

function sortApiParameters(parameters) { 
  var sortedParameters = parameters.sort();
  // Logger.log("sortApiParameters() " + sortedParameters);

  return sortedParameters;
}

/** 

function getApiExpirationTime() {
  var expiration = Date.now() + 10 * 60 * 1000;
  // Logger.log("getApiExpirationTime() " + expiration);

  return expiration;
}

/** 
 * Returns API Signature calculated using api_secret. 
 */
function getApiSignature(expires, sheetName) {
  var parameters = getApiParameters(expires, sheetName);
  var sortedParameters = sortApiParameters(parameters).join('') + API_SECRET;
  // Logger.log("Sorted Parameters  " + sortedParameters);

  var digest = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, sortedParameters);

  var signature = '';
  for (j = 0; j < digest.length; j++) {
      var hashVal = digest[j];
      if (hashVal < 0) hashVal += 256; 
      if (hashVal.toString(16).length == 1) signature += "0";
      signature += hashVal.toString(16);
  }

  return signature;
}



/** 
 *********************************************************************************
 * Date helpers
 *********************************************************************************
 */

// Returns today's date string in Mixpanel date format '2013-09-11'
function getMixpanelDateToday() {
  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth() + 1; 
  var yyyy = today.getFullYear();

  if (dd < 10) {
    dd = '0' + dd;
  } 
  if ( mm < 10 ) {
    mm = '0' + mm;
  } 

  today = yyyy + '-' + mm + '-' + dd;
  return today;
}

// Returns yesterday's's date string in Mixpanel date format '2013-09-11'
function getMixpanelDate(days){
  var today = new Date();
  var d = new Date(today);
  d.setDate(today.getDate() - days);

  //Logger.log(yesterday);
  var dd = d.getDate();
  //Logger.log(yesterday);
  var mm = d.getMonth() + 1;
  var yyyy = d.getFullYear();

  if (dd < 10) {
    dd = '0' + dd;
  }
  if (mm < 10) {
    mm = '0' + mm;
  }

  d = yyyy + '-' + mm + '-' + dd;
  //Logger.log(yesterday);
  return d;
}
*步骤1)在此处填写您帐户的Mixpanel信息
`在这里输入代码`*/
var API_KEY='*****';
var API_SECRET='**';
/**
*步骤2)定义要在电子表格中创建新图纸的选项卡。
*0创建一个新工作表作为第一个工作表。
*1在第二张图纸处创建新图纸,以此类推。
*/
var CREATE_NEW_SHEETS_AT=0;
/**
*步骤3)将日期范围定义为“yyyy-mm-dd”或“2013-09-13”格式的字符串
*
*今天的日期:设置为等于getMixpanelDateToday()
*昨天的日期:设置为等于GetMixPanelDateDateDateDateDate()
*/
var FROM_DATE=getMixpanelDate(7);
var TO_DATE=getMixpanelDate(1);
/**
*步骤4)定义分段查询-获取事件的数据,按属性进行分段和筛选。
变量API_参数={
'公式':['QuestionAsquired','(属性[“InputMethod”])==“Voice”或(属性[“InputMethod”])==“Text”、“general”、“day”、“B7”],
'公式1':['QuestionAsquired','(属性[“InputMethod”])=“Voice”或(属性[“InputMethod”])=“Text”、'unique'、'day'、'B2'],
//'公式2':['QuestionResponsed','(属性[“InputMethod”])=“Voice”或(属性[“InputMethod”])=“Text”、'unique'、'day'、'B3'],
};
//迭代查询的哈希映射,获取数据,将其写入电子表格
函数getMixpanelData(){
for(API_参数中的变量i)
{
var cell=API_参数[i][4];
获取数据(i,单元);
}
}
//在电子表格中创建一个菜单,方便用户访问上述功能
函数onOpen(){
var activeSpreadsheet=SpreadsheetApp.getActiveSpreadsheet();
activeSpreadsheet.addMenu(
“混合面板”[{
名称:“获取Mixpanel数据”,函数名称:“getMixpanelData”
}]);
}
/**
*从mixpanel api获取数据并插入到电子表格
*
*/
函数fetchMixpanelData(sheetName,单元格){
var c=细胞;
var expires=GetApiExpireationTime();
var urlParams=getApiParameters(expires,sheetName).join(“&”)
+“&sig=“+getApiSignature(过期,sheetName);
//为可能产生“无效参数”错误的特殊字符添加URL编码。
//由于%符号,应始终首先对模数进行编码。
urlParams=urlParams.replace(/\%/g,'%25');
urlParams=urlParams.replace(/\s/g,'%20');
urlParams=urlParams.replace(/\[/g,'%5B');
urlParams=urlParams.replace(/\]/g,'%5D');
urlParams=urlParams.replace(/\“/g,'%22');
urlParams=urlParams.replace(/\(/g,'%28');
urlParams=urlParams.replace(/\)/g,'%29');
urlParams=urlParams.replace(/\>/g,'%3E');

urlParams=urlParams.replace(/\n您应该只提供再现问题所需的最低代码,而不是整个工作脚本。您似乎还注释掉了部分代码-这是故意的吗?也许这就是问题的根源?您应该只提供再现问题所需的最低代码,而不是整个工作脚本似乎你也注释掉了这段代码的一部分-这是故意的吗?也许这就是你问题的根源?