【问题标题】:Is there a way to average only some rows of data in a sheet有没有办法平均工作表中的某些数据行
【发布时间】:2021-08-09 19:48:14
【问题描述】:

我有几张表格,可以根据不同区域的文件审查导入各种分数。我想计算那些在每个时期内接受过多次审查的办事处的平均办事处,但无法提前知道哪些办事处将拥有超过一次审查,因此在每个列表中可能有

  • office 1 分
  • office 2 分数
  • office 2 分数
  • office 3 得分 等等。 有没有办法自动执行此操作,例如查找重复项和平均值,还是我必须查看导入并手动完成? 干杯:) 梅格

【问题讨论】:

标签: google-sheets google-sheets-formula average


【解决方案1】:

您可以在表格中使用query 函数。

把这个放在单元格E1:

=query(A:C,"select B,avg(C) where B is not null group by B label avg(C) 'Office average' ",1)

【讨论】:

    【解决方案2】:
    function getDataSubset() {
      const ss = SpreadsheetApp.getActiveSpreadsheet()
      const ssId = ss.getId();
      const sheet = ss.getSheetByName('contact')
      const sheetName = sheet.getName()
      const lastRow = sheet.getLastRow();
      const lastCol = sheet.getLastColumn();
      
    //  let theQuery = "SELECT * WHERE job ='a job'"   // works
      let theQuery = "SELECT A, B WHERE E ='a job' AND G > 30"   //works
      
    // with header row - result is an array of objects if header row is specified
    //  const a1Range = sheet.getDataRange().getA1Notation();
    //  let result = Utils.gvizQuery(
    //     ssId       // YOUR_SPREADSHEET_ID
    //    ,theQuery
    //    ,sheetName  // can be a number (the sheetId), or the name of the sheet; if not needed, but headers are, pass in undefined
    //    ,a1Range    // specify range, ex: `A2:O`
    //    ,1          // HEADER_ROW_INDEX_IF_NEEDED> - always a number
    //  );
      
    // no header row  -  result is an array of arrays
      const a1Range = sheet.getRange(2,1,lastRow,lastCol).getA1Notation();
      let result = Utils.gvizQuery(
         ssId       // YOUR_SPREADSHEET_ID
        ,theQuery
        ,sheetName  // can be a number (the sheetId), or the name of the sheet; if not needed, but headers are, pass in undefined
        ,a1Range    // specify range, ex: `A2:O`
                    // HEADER_ROW_INDEX_IF_NEEDED> - always a number
      );
      console.log( JSON.stringify(result) );
    }
    
    
    /**
     * https://stackoverflow.com/questions/51327982/how-to-use-google-sheets-query-or-google-visualization-api-from-apps-script/51328419#51328419
     */
    (function(context) {
    
        const Utils = (context.Utils || (context.Utils = {}));
    
    
        /**
         * Queries a spreadsheet using Google Visualization API's Datasoure Url.
         *
         * @param        {String} ssId    Spreadsheet ID.
         * @param        {String} query   Query string.
         * @param {String|Number} sheetId Sheet Id (gid if number, name if string). [OPTIONAL]
         * @param        {String} range   Range                                     [OPTIONAL]
         * @param        {Number} headers Header rows.                              [OPTIONAL]
         */
        Utils.gvizQuery = function(ssId, query, sheetId, range, headers) {
            var response = JSON.parse( UrlFetchApp
                    .fetch(
                        Utilities.formatString(
                            "https://docs.google.com/spreadsheets/d/%s/gviz/tq?tq=%s%s%s%s",
                            ssId,
                            encodeURIComponent(query),
                            (typeof sheetId === "number") ? "&gid=" + sheetId :
                            (typeof sheetId === "string") ? "&sheet=" + sheetId :
                            "",
                            (typeof range === "string") ? "&range=" + range :
                            "",
                            "&headers=" + ((typeof headers === "number" && headers > 0) ? headers : "0")
                        ), 
                        {
                            "headers":{
                                "Authorization":"Bearer " + ScriptApp.getOAuthToken()
                            }
                        }
                    )
                    .getContentText()
                    .replace("/*O_o*/\n", "") // remove JSONP wrapper
                    .replace(/(google\.visualization\.Query\.setResponse\()|(\);)/gm, "") // remove JSONP wrapper
                ),
                table = response.table,
                rows;
    
            if (typeof headers === "number") {
    
                rows = table.rows.map(function(row) {
                    return table.cols.reduce(
                        function(acc, col, colIndex) {
                            acc[col.label] = row.c[colIndex] && row.c[colIndex].v;
                            return acc;
                        }, 
                        {}
                    );
                });
    
            } else {
    
                rows = table.rows.map(function(row) {
                    return row.c.reduce(
                        function(acc, col) {
                            acc.push(col && col.v);
                            return acc;
                        },
                        []
                    );
                });
            }
    
            return rows;
    
        };
    
        Object.freeze(Utils);
    
    })(this);
    

    【讨论】:

    • 嗨,aNewb,谢谢你,但我不在我理解的谷歌表格级别,哈哈。我认为它看起来像是在使用应用程序脚本,很遗憾我无权编辑。
    • 任何拥有 gmail 帐户的人都可以使用应用程序脚本。它是有史以来最简单的语言之一。
    猜你喜欢
    • 2020-11-27
    • 2013-10-15
    • 1970-01-01
    • 2012-05-02
    • 2020-05-18
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 2012-01-11
    相关资源
    最近更新 更多