如何从Google Sheets建立一个自定义的报告

问题描述 投票:0回答:1

我在网上搜索了一下从数据电子表格建立报表的方法,好像是 谷歌数据工作室谷歌表 是实现这一目标的一个选择。

尽管在LibreOffice Calc上有一定的经验,但我在Google Sheets和DataStudio上是新手。

我在Google Sheets的数据是。

enter image description here

我想建立的报告,其必须更新时,新的数据将到达谷歌表(比方说,每一天或左右)。

enter image description here

我只是彩色的字段名(objId; objDesc; objMore; objProgress; taskId; taskDesc; taskMore; event。),并将数据整理成电子表格,便于理解我想实现的目标。

欣赏你的帮助。

google-sheets google-data-studio
1个回答
1
投票

解决办法

因此,基本上,你想生成一个报告,你的对象,任务和事件的层次结构,每当你点击一个按钮的描述顺序。

在这个解决方案中,我提供了最复杂的部分所需的代码,然后为其余步骤提供指导。

  1. 在您的工作表中添加一个按钮,其数据为 插入->图纸 并绘制你的按钮。
  2. 创建一个新的工作表并插入以下公式,如图所示。
=iferror(ArrayFormula(lookup(unique(Sheet1!A1:A),Sheet1!A1:A,row(Sheet1!A1:A)+1)))

=iferror(ArrayFormula(lookup(unique(Sheet1!E2:E),Sheet1!E2:E,row(Sheet1!E2:E)+1)))

这些函数基本上会检测指定列(在我们的例子中是对象ID和任务ID的列)中任何值的变化,这将有助于生成报告。

enter image description here

  1. 在这之后,前往脚本编辑器,使用下面的函数来生成一个具有正确数据分级排序的对象数组。下面这段代码包含了自我解释的注释。这是最棘手的部分,它利用之前创建的表来知道我们的对象和任务ID在哪里变化,并将正确的信息存储在正确的地方。

对于这个数据。

enter image description here

这将是运行函数后生成的对象。

 [{ObjectId=1.0, 1={TaskId=S02, Events=[[3242.0]], TaskMore=ppp, Description=ppp}, 0={TaskId=S01, Description=qqq, Events=[[1.0], [23.0], [324.0]], TaskMore=qqq}, ObjectMore=aaaa, ObjectDesc=aaaa, 2={TaskMore=lll, Events=[[3.0]], TaskId=S03, Description=lll}, ObjectProgress=0.7}, {3={Description=www, Events=[[43.0]], TaskMore=www, TaskId=T01}, 5={TaskId=T03, Events=[[5.0]], TaskMore=ttt, Description=ttt}, ObjectMore=bbbb, 4={Description=eee, TaskId=T02, TaskMore=eee, Events=[[54.0], [4.0]]}, ObjectDesc=bbbb, 6={Description=yyy, Events=[[345.0], [343.0]], TaskId=T04, TaskMore=yyy}, ObjectProgress=0.33, ObjectId=2.0}]

function myFunction() {
  // Get sheets
  var sheetData = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  var sheetHelp = SpreadsheetApp.getActive().getSheetByName('Sheet2');
  
  // This is the array where we will be storing our info
  var Objects = []
  // Get the ranges of values from our helper sheet that detect changes in that column
  var ObjectIds = sheetHelp.getRange(2, 1,sheetHelp.getLastRow(),1).getValues().flat();
  var TaskIds = sheetHelp.getRange(2, 2,sheetHelp.getLastRow(),2).getValues().flat();
  
  // Delete all the blank elements in the flaten array of values
  ObjectIds = ObjectIds.filter(item => item);
  TaskIds = TaskIds.filter(item => item);
  
  // Delete the last item as this would just be the change between a value and a blank cell
  ObjectIds.pop();
  TaskIds.pop();

  // Lets iterate through all our main objects detected
  for(i=0;i<ObjectIds.length;i++){
    // for each object insert its details and information
    Objects.push({
      ObjectId: sheetData.getRange(parseInt(ObjectIds[i]), 1).getValue(),
      ObjectDesc : sheetData.getRange(parseInt(ObjectIds[i]), 2).getValue(),
      ObjectMore : sheetData.getRange(parseInt(ObjectIds[i]), 3).getValue(),
      ObjectProgress : sheetData.getRange(parseInt(ObjectIds[i]), 4).getValue()   
    });
    // for each object iterate through all its possible tasks
    for(j=0;j<TaskIds.length;j++){
      // get the last row value as for the last element of the tasks if we do not have anything it will 
      // have issues in the following if condition if this is null
      var lastRow = sheetHelp.getLastRow();
      ObjectIds.push(lastRow);
      
      // If the task is within the object index range (i.e for an object between indices 8 and 12
      // his tasks are those between these indices as well
      if(TaskIds[j]<ObjectIds[i+1] && TaskIds[j]>=ObjectIds[i]){
        TaskIds.push(lastRow);
        
        // get all the events for this specific task checking that they are within the indices
        // of the task itself (only events that are within the index boundaries of the task
        var events = sheetData.getRange(TaskIds[j], 8,TaskIds[j+1]-TaskIds[j],1).getValues();
        
        // add the new task object
        Objects[i][j] = {
          TaskId : sheetData.getRange(parseInt(TaskIds[j]),5).getValue(),
          Description : sheetData.getRange(parseInt(TaskIds[j]),6).getValue(),
          TaskMore : sheetData.getRange(parseInt(TaskIds[j]),7).getValue(),
          Events : events
        };
        TaskIds.pop();
      }
      ObjectIds.pop();
    }
  }  
}
  1. 回到你创建的按钮,在它的选项下。分配脚本 选择我们的功能 myFunction.
  2. 一旦你有了这个具有正确层次信息的对象数组,那么就看你想用它来生成报表的方式了。你可以

    • 使用JSON对象数组来生成HTML电子邮件,以便在你点击工作表按钮时通过Gmail发送报告。
    • 生成一个新的工作表,并将这些数据粘贴到图中所示的结构中(现在你已经对数据进行了分类,更容易实现自动化),导出为PDF,并删除这个工作表,因为它的目的只是为了生成这个PDF报告。
    • 把它作为JSON的数组发送给其他服务,然后他们可以很容易地使用。

我希望这对你有所帮助。如果你还需要什么,或者你有什么不明白的地方,请告诉我 :)

© www.soinside.com 2019 - 2024. All rights reserved.