首页 > 解决方案 > 在 Google Data Studio 中合并列数据

问题描述

我对 GDS 还很陌生,到目前为止我很喜欢它。我想要做的是有一个包含特定工作要求和数量的谷歌表,然后使用 Data Studio 能够按要求搜索并在同一字段中拥有数量。

我已经模拟了一些我目前拥有的示例:

https://datastudio.google.com/open/1z0SIYa0ucpBiJXf_IQBaIsu61INP71Hc

https://docs.google.com/spreadsheets/d/1eUhE3chM77etyTcv4E7jyB_6vCFxSYArMyF2ptxK180/edit?usp=sharing

如果我将数量与材料结合起来,我将无法在数据工作室中通过该材料进行搜索。我希望能够只有两列,并且有人在 Mat X 中输入,并且所有与之相关的工作都在同一列中提供数量。

我知道我可以通过为每种材料设置 N 列数量并包含数量但对我的应用程序不实用来完成类似的操作。

谢谢你的时间。

标签: google-sheetsgoogle-data-studio

解决方案


Data Studio 无法对 Material 进行过滤,因为它无法识别唯一的组件。

我建议您在由脚本/宏创建的电子表格中创建第二个表,其中按作业列出材料和数量。此工作表是 Data Studio 的来源,因此它将启用按作业或材料进行过滤。可以通过运行宏随时更新表。

该表可能如下所示: 在此处输入图像描述

Data Studio 中的输出如下所示: 在此处输入图像描述

这是Data Studio 的示例

如果是这个 Excel,VBA 会很容易编写,但 Google Scripts 使用 Javascript,所以我肯定会花更长的时间(尽管其他人会更熟练)来提供代码示例来实现新的电子表格。

Google Scripts 的逻辑是:
对于每个工作

  1. 计算需求中逗号的数量。需求数量 = CommaCount 加一。
  2. 解析需求以获取数量和材料详细信息
  3. 将第一个数量/材料复制到此作业的 C 和 D 列。
  4. 对于每个附加要求,复制工作行,并将附加数量/材料复制到相应的 C 和 D 列。

在数据洞察中

  1. 插入表格;维度:职位、要求;排序:升职。
  2. 插入过滤器;维度:工作;排序:升职。
  3. 插入过滤器;尺寸:材质;排序:材料升序。

更新 此代码实现了上述目标 - 拥有第二张服务 GDS 的工作表。它获取原始作业信息并构建一张数据表,识别每个作业的材料和数量的每种组合。代码可能没有它可能的效率那么高,但在这个阶段更重要的是——它可以工作。

三个假设: 1) 每个工作总是不止一种材料;2) material-code 不包含空格;3)数量总是一个整数。

快速总结
有一个“主”功能可以分配给 GDS_data 表上的按钮。这使得重建 GDS 数据变得非常容易。功能是:

  • BuildGDSData - 主要功能
  • CountJobs - 计算,嗯....,工作的数量
  • CountMaterials - 计算每个作业的需求数量
  • CreateJobRows - 插入新行,以便每个作业每个要求有一行
  • BuildMaterials - 解析作业要求,并为作业的每一行填充“材料”和“数量”列。

新的数据洞察页面


// Convert Job info for GDS 
function BuildGDSData() {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheetByName('jobinfo');// assumes basic job info is on sheet called "jobinfo"
var target = ss.getSheetByName('gds_data');// assumes that the GDS data is build on a sheet called "gds_data"

// Start gds_data from scratch. Delete everything
target.clearContents();
target.clearFormats();

// Get the data range from sheet = jobinfo
var rangeToCopy = source.getRange(1, 1, source.getMaxRows(),source.getMaxColumns());

// Paste the data to the cheet=gds_data 
rangeToCopy.copyTo(target.getRange(1, 1));

// Add headings on sheet=gds_data for Material and Qty
target.getRange('C1').setValue('Material');
target.getRange('D1').setValue('Qty'); 

// Move the cursor to cell B2 on sheet = gds_data- this is the start of the job information
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getRange('B2:B2');
sheet.setActiveRange(range);

// Call function CountJobs; assign number to jobs to variable "job_count"
var job_count = CountJobs();

// execute loop for the number of jobs
for (var i = 0; i < job_count; i++) {     

// Call function CountMaterials; assign the number of materials for the job to variable MatCount
var MatCount = CountMaterials();

// Copy and insert new rows for the job so that there is one row per requirement. Copy the job details onto each new row
// Number of rows to create =  for this job, the number of materials minus one. The "one" is row of job information that already exists.
CreateJobRows ((MatCount-1));

// Parse the requirements and copy the results to the Materials and Qty columns
BuildMaterials();  
}
}


// Count and return the number of Jobs = number of rows of data minus one. the "one" is the header row,
function CountJobs() {
var numRows = SpreadsheetApp.getActiveSpreadsheet().getLastRow();
var numCols = SpreadsheetApp.getActiveSpreadsheet().getLastColumn();  
return (numRows-1);
}


// Count and return the number of Materials for the current job; Quantity = number of commas plus 1
function CountMaterials(){
var activeSheet = SpreadsheetApp.getActiveSheet();
var selection = activeSheet.getSelection();

// get the requirments value for this job
var text = selection.getCurrentCell().getValue();

// use the split command to explode the requirments. Split by comma.
var textArray = text.split(",");

// Couht the number of components created by the split
var Num_Materials = textArray.length;

// Return the number of Components
return   Num_Materials;
}


// Create new rows to cater for each requirment on the job. The count variable identifies the number of rows to create
function CreateJobRows(count) {
var sheet = SpreadsheetApp.getActiveSheet();

for (var i = 0; i < count; i++) { 
// get the current row number
row = sheet.getActiveCell().getRow();

// insert a new row after the current row
sheet.insertRowAfter(row);

//copy the contents from cells A and B of the current row to the new row
var rangeToCopy = sheet.getRange(row, 1, 1, 2);
rangeToCopy.copyTo(sheet.getRange(row+1, 1));

}

}


// Parse the requirements and copy the results to the Materials and Qty columns
function BuildMaterials(){

var activeSheet = SpreadsheetApp.getActiveSheet();
var selection = activeSheet.getSelection();

// Before you start, get the current row and current column and assign the valuares to variables.
job_row = activeSheet.getActiveCell().getRow();
job_col = activeSheet.getActiveCell().getColumn();

// Get the requirements for this job
var text = selection.getCurrentCell().getValue();

// Split by comma and put the requirments into an array
var textArray = text.split(",");

// Count the number of components in the array
var NumRequirements = textArray.length;

for (var i = 0; i < NumRequirements; i++) {

// establish some variables and ensure that values from a previous loop don't carry over
var req_string = "";
var req_array = [];
var req_data = [];
var qty="";
var material = "";

// get the component; trim just in case
req_string = textArray[i].trim();

// put the component into its own array (req_array)
var req_array = req_string.split(" ");

// get values for quanity and material
qty = req_array[0];
material= req_array[1];

// assign values for quanty and material type to new array. Order of Qty and Material is different.
req_data[0] = material;
req_data[1] = qty;

// create array in format that Google Sheets requires.
var req_results = [req_data];

// define range to copy the results
ResultsRange = activeSheet.getRange((job_row+i), (job_col+1), 1, 2);; // getRange(row, column, numRows, numColumns)

// insert values for Materials and Qty into the respective columns on the relevant row.
ResultsRange.setValues(req_results);

}  // repeat for next requirement

// Finished requirements for this job.
// Move the cursor down to the next job (rows=Number of Requirements)
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getRange((job_row+NumRequirements), 2, 1, 1); // getRange(row, column, numRows, numColumns)
sheet.setActiveRange(range);
}


推荐阅读