使用来自其他单元格的数据透视数据处理每一行

Processing each Row with pivot data from other cell

==================================

2019 年 12 月 11 日更新 我的问题更多是关于宏脚本

目标(图中)

更改为原始 sheet:

更易读的格式:

基本上,我所做的是将活动名称与分隔符分开并进行解析。 如果函数只处理单个单元格,我没有问题,例如:

在 "Report" Sheet CELL B2 上,仅从 "Data" B2 获取数据

当 return 数据需要涉及特定条件的条件运算符时,我遇到了问题。因此,在处理单元格 B2 时,它需要来自 E2、D2 等的内容

=====================================

我正在从 Google Ads/Analytics API 到 Google Sheet 的特定工作sheet 中获取数据(我称之为' 原始数据')。 现在我正在为广告系列使用模式,因此我可以轻松 split/break 使用分隔符来获取特定数据。

例如:

有了这个,通过使用下划线作为分隔符,我可以将活动名称拆分为各种数据:

活动Objective:销售额

活动标题:TBMB

网络:SEM

分支:全部

定位:关键字

..等等

然后我创建新的 sheet 称为 CReport,它包含来自原始数据 sheet 的相同数据,但对于营销人员而言可视化效果更好。

现在,在 Google 上搜索后,我找到了自引用单元格的解决方案。 脚本是这样的:

function getSegment(data,index){
  temp=data.split("_");
  return temp[index-1];
}

function dataParse(input,dataSegment){
  return Array.isArray(input) ? input.map(function(e){
    return e.map(function(f){
      if(f!=""){
        return getSegment(f,dataSegment);
      }
    }
    )}
  ) : "false usage";
}

因此,如果我想要一个包含网络名称的列,我可以将此公式放在第 2 行(因为第 1 行用于 table header),如下所示:

=ArrayFormula(dataParse('RAW DATA'!B2:B;2))

现在我的问题是: 这适用于 self-reference 单元格,意味着如果从原始数据 sheet 中的 B2 获取数据,它将是活动报告 sheet.

中单元格引用的唯一数据

如果指针在 CReport 上的 B2 中 Sheet 不仅需要来自 RAW DATA 中的 B2 中的数据,还需要来自 D2 Cell 中的数据。

我需要在函数中添加什么脚本? 我期待代码块会像这样

function dataParse(input,dataSegment){
  return Array.isArray(input) ? input.map(function(e){
    return e.map(function(f){
      if(f!=""){
        segmentData=getSegment(f,dataSegment);
        if(segmentData=="google"){
          returnData=get reference from column D //<---
        }else{
          returnData=get reference from column E //<---
        }
        return returnData
      }
    }
    )}
  ) : "false usage";
}

希望它够清楚。 提前致谢!

我这样修改了你的函数:

// range (String): It will be used to get the info in a range
function dataParse(input,dataSegment, range){
  var val = "";
  return Array.isArray(input) ? input.map(function(e, index){
    return e.map(function(f){
      if(f!=""){
        // If col D has value google then take info from col B
        if(f === "google") val = getDesiredRangeValue("B", range, index);
        // else take info from col E
        else val = getDesiredRangeValue("E", range, index);
        // Take segment as needed 
        return getSegment(val,dataSegment);
      }
    }
    )}
  ) : "false usage";
}

为了让它工作,我向函数插入了一个额外的参数。现在您需要在 ArrayFormula 中以 A1 表示法将范围作为字符串传递,这是因为 input 参数只为您提供中的值单元格,并且有了这个额外的参数,就有可能获得额外的信息。为了使其正常工作,请始终使用与下一个示例所示相同的范围:

=ArrayFormula(dataParse('RAW DATA'!D2:D5, 2,"D2:D5"))

=ArrayFormula(dataParse('RAW DATA'!D2:D, 2,"D2:D"))

注意我还添加了一个名为 getDesiredRangeValue 的新函数,它将从您需要的列中获取值,具体取决于 Col D[ 中的一个单元格=52=] 的值为 google。这是函数的样子:

/*
//  A1 (String): The col from where you will want the info
//  range (String): It will be used to get the info in a range 
//  index (Integer): It gives the index number from the main array gotten in the input arg
*/
function getDesiredRangeValue(A1, range, index){
  var rowNumbers = range.match(/\d+/g);
  // It checks if the range will has and end or it will prolong without specifying and end row
  if(rowNumbers.length > 1){
   var rangeCol = ss.getRange(A1 + rowNumbers[0] + ":" + A1 + rowNumbers[1]).getValues();
  } else {
   var rangeCol = ss.getRange(A1 + rowNumbers[0] + ":" + A1).getValues();
  }
  // It returns the whole value from each cell in the specified col
  return rangeCol[index][0];
}

代码

现在您的整个代码将如下所示:

// Global var
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("RAW DATA");

function getSegment(data,index){
  temp=data.split("_");
  return temp[index-1];
}

/*
//  A1 (String): The col from where you will want the info
//  range (String): It will be used to get the info in a range 
//  index (Integer): It gives the index number from the main array gotten in the input arg
*/
function getDesiredRangeValue(A1, range, index){
  var rowNumbers = range.match(/\d+/g);
  // It checks if the range will has and end or it will prolong without specifying and end row
  if(rowNumbers.length > 1){
   var rangeCol = ss.getRange(A1 + rowNumbers[0] + ":" + A1 + rowNumbers[1]).getValues();
  } else {
   var rangeCol = ss.getRange(A1 + rowNumbers[0] + ":" + A1).getValues();
  }
  // It returns the whole value from each cell in the specified col
  return rangeCol[index][0];
}

// range (String): It will be used to get the info in a range
function dataParse(input,dataSegment, range){
  var val = "";
  return Array.isArray(input) ? input.map(function(e, index){
    return e.map(function(f){
      if(f!=""){
        // If col D has value google then take info from col B
        if(f === "google") val = getDesiredRangeValue("B", range, index);
        // else take info from col E
        else val = getDesiredRangeValue("E", range, index);
        // Take segment as needed 
        return getSegment(val,dataSegment);
      }
    }
    )}
  ) : "false usage";
}

文档

这些是我用来帮助你的文档: