在Google Data Studio中合并列数据

时间:2018-09-17 21:52:07

标签: google-sheets google-data-studio

我对GDS还是陌生的,到目前为止我很喜欢它。我正在尝试做的是创建一个Google表格,其中包含特定工作的需求和数量,然后使用Data Studio来按需求进行搜索并将数量显示在同一字段中。

我已经模拟了一些我目前拥有的东西的例子:

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

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

如果将数量与物料结合在一起,则将失去在数据工作室中按物料进行搜索的能力。我希望只能有两列,有人在Mat X中键入内容,并且所有与之相关的工作都在同一列中。

我知道我可以通过为每种材料使用N个数量的列来完成类似的事情,并且包含该数量但对于我的应用而言不实际。

谢谢您的时间。

1 个答案:

答案 0 :(得分:1)

Data Studio无法筛选Material,因为它无法识别唯一的组件。

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

该表可能如下所示: enter image description here

data Studio中的输出如下所示: enter image description here

这里是sample of Data Studio

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

Google脚本逻辑为:
对于每个工作

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

在Data Studio中

  1. 插入表格;维度:工作,需求;排序:升序。
  2. 插入过滤器;维度:工作;排序:升序。
  3. 插入过滤器;尺寸:材料;排序:升序。

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

三个假设:1)每个作业始终使用一种以上的材料; 2)物料代码不包含空格; 3)数量始终是整数。

快速摘要
可以将一个“主”功能分配给GDS_data工作表上的一个按钮。这使得重建GDS数据极为容易。 这些功能是:

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

新的Data Studio Page


// 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);
}