Google sheets 在Google Data Studio中合并列数据

Google sheets 在Google Data Studio中合并列数据,google-sheets,google-data-studio,Google Sheets,Google Data Studio,我是GDS的新手,到目前为止我很喜欢它。我想做的是用一个包含特定工作的需求和数量的Google表单,然后使用datastudio能够根据需求进行搜索,并将数量放在同一个字段中 我已经模拟了一些我目前拥有的例子: 如果我将数量与材料相结合,我将失去在DataStudio中按该材料进行搜索的能力。我希望能够有两列,有人在Mat X中输入,所有的工作都会在同一列中给出数量 我知道,我可以通过为每种材料设置N个列来完成类似的任务,并设置包含数量的列,但对于我的应用程序来说并不实用 谢谢你抽出时间

我是GDS的新手,到目前为止我很喜欢它。我想做的是用一个包含特定工作的需求和数量的Google表单,然后使用datastudio能够根据需求进行搜索,并将数量放在同一个字段中

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

如果我将数量与材料相结合,我将失去在DataStudio中按该材料进行搜索的能力。我希望能够有两列,有人在Mat X中输入,所有的工作都会在同一列中给出数量

我知道,我可以通过为每种材料设置N个列来完成类似的任务,并设置包含数量的列,但对于我的应用程序来说并不实用


谢谢你抽出时间

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

我建议您在电子表格中创建第二个表格,该表格由脚本/宏创建,并按作业列出材料和数量。 此工作表是DataStudio的源,因此它将支持按作业或材质进行筛选。可以通过运行宏随时更新该表

该表可能如下所示:

data Studio中的输出如下所示:

这是一个例子

如果有这样的Excel,VBA将很容易编写,但是Google脚本使用Javascript,因此我需要更长的时间(尽管其他人会更熟练)来提供实现新电子表格的代码示例

谷歌脚本的逻辑是:
每项工作

  • 计算需求中逗号的数量。需求数量=通信量加一
  • 分析需求以获取数量和物料详细信息
  • 将第一个数量/材料复制到此作业的C&D列中
  • 对于每个附加要求,复制作业行,并将附加数量/材料复制到相应的C&D列中

  • 在DataStudio中

  • 插入表格;维度:工作、需求;排序:作业升序
  • 插入过滤器;维度:工作;排序:作业升序
  • 插入过滤器;尺寸:材料;排序:物料升序

  • 更新 该代码实现了上面概述的目标——为GDS提供第二张表单。它获取原始作业信息并构建一张数据表,确定每个作业的材料和数量组合。代码可能没有它可能的那么有效,但在这个阶段更重要的是——它可以工作

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

    快速摘要
    有一个“主”功能可以分配给GDS_数据表上的按钮。这使得重建GDS数据非常容易。 功能包括:

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


    //转换GDS的作业信息
    函数BuildGDSData(){
    var ss=SpreadsheetApp.getActiveSpreadsheet();
    var source=ss.getSheetByName('jobinfo');//假设基本作业信息位于名为“jobinfo”的工作表上
    var target=ss.getSheetByName('gds_data');//假设gds数据构建在名为“gds_data”的工作表上
    //从头开始启动gds_数据。删除所有内容
    target.clearContents();
    target.clearFormats();
    //从sheet=jobinfo获取数据范围
    var rangeToCopy=source.getRange(1,1,source.getMaxRows(),source.getMaxColumns());
    //将数据粘贴到cheet=gds\u数据
    copyTo(target.getRange(1,1));
    //在表上添加标题=物料和数量的gds\U数据
    target.getRange('C1')。setValue('Material');
    target.getRange('D1').setValue('Qty');
    //将光标移动到sheet=gds_data上的单元格B2-这是作业信息的开始
    var sheet=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    var范围=sheet.getRange('B2:B2');
    表.setActiveRange(范围);
    //调用函数CountJobs;将作业编号分配给变量“job\u count”
    var job_count=CountJobs();
    //针对作业数执行循环
    对于(var i=0;i// 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);
    }