Data Studio 无法过滤 Material,因为它无法识别唯一组件。
我建议您在电子表格中创建第二个表,该表由脚本/宏创建,并按作业列出材料和数量。
此工作表是 Data Studio 的来源,因此它将启用按作业或材料进行过滤。可以通过运行宏随时更新表格。
表格可能如下所示:
Data Studio 中的输出如下所示:
这是sample of Data Studio。
如果是 Excel,VBA 将很容易编写,但 Google Scripts 使用 Javascript,因此我肯定需要更长的时间(尽管其他人会更熟练)来提供代码示例来实现新的电子表格。
Google 脚本的逻辑是:
对于每个工作
- 计算需求中逗号的数量。需求数量 = CommaCount 加一。
- 解析需求以获取数量和材料详细信息
- 将第一个数量/材料复制到此作业的 C 和 D 列中。
- 对于每个附加要求,复制作业行,并将附加数量/材料复制到相应的 C 和 D 列中。
在数据洞察中
- 插入表格;维度:职位、要求;排序:升职。
- 插入过滤器;维度:工作;排序:升职。
- 插入过滤器;尺寸:材质;排序:材料升序。
更新
此代码实现了上述目标 - 拥有为 GDS 提供服务的第二张工作表。它获取原始作业信息并构建一张数据表,识别每个作业的材料和数量的每种组合。代码可能没有它想象的那么高效,但在这个阶段更重要的是——它可以工作。
三个假设:1)每个工作总是不止一种材料; 2) material-code 不包含空格; 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);
}