是否可以在Google电子表格中了解过滤器的行结果?

时间:2017-02-19 09:55:55

标签: google-apps-script google-spreadsheet-api

我想知道Google App脚本中过滤器行结果但是我没有获得它们,我总是获得所有选择的行或表格。我已经调查了,似乎这个API是不可能的。你知道它是真的还是存在获得这些行的另一种方式? THX

function docReport() {
  try {
    splashScreen("Generando informe...");    
    var activeSheet = SpreadsheetApp.getActiveSheet();    
    var numberOfColumns = activeSheet.getLastColumn();
    var numberOfRows = activeSheet.getLastRow(); 
    var activeRow = activeSheet.getRange(1, 1, numberOfRows, numberOfColumns).getValues();

    var docReport =  DocumentApp.create(REPORT_FILE_NAME);
    var bodyReport = docReport.getBody();
    bodyReport.setAttributes(stylePage);

    for (var row = 1; row < numberOfRows; row++) {
      if (!isEmpty(activeRow[row], numberOfColumns)) {
        var image = UrlFetchApp.fetch(IMG_BBVA);
        /*var paragraph = bodyReport.appendParagraph("");
        paragraph.addPositionedImage(image).setHeight(100).setWidth(98);
        paragraph.appendText("\r\n");
        paragraph.setAttributes(styleTitle);*/

        //bodyReport.appendImage(image).setHeight(100).setWidth(98); //Incluimos la imagen de cabecera   

        bodyReport.appendParagraph("").addPositionedImage(image).setHeight(100).setWidth(98).setLayout(DocumentApp.PositionedLayout.WRAP_TEXT);
        bodyReport.appendParagraph("          SERVICIOS JURÍDICOS").setAttributes(styleTextBlue); 
        bodyReport.appendParagraph(DATE_REPORT + "\r\n\r\n").setAttributes(styleDate); 

        for (var col = 1; col < numberOfColumns; col++) { //Ignoramos la columna 1 que es el contador
          if (activeRow[row][col] != "") { //Si el valor es vacio no lo imprimimos
            if (activeRow[0][col] == "NORMA")
              bodyReport.appendParagraph(String(activeRow[row][col]).trim()).setAttributes(styleTextNorma); //Incluimos contenido
            else { 
              bodyReport.appendParagraph(activeRow[0][col] + ":").setAttributes(styleTitle); //Incluimos título
              bodyReport.appendParagraph(String(activeRow[row][col]).trim()).setAttributes(styleText); //Incluimos contenido
            }
            bodyReport.appendParagraph("");
          }
        }
        bodyReport.appendPageBreak(); 
      }
    } 
    bodyReport.appendPageBreak();     
    docReport.saveAndClose();
    MailApp.sendEmail(Session.getActiveUser().getEmail(), "Informe " + REPORT_FILE_NAME + " generado", 
                      'Se ha creado un nuevo informe "' + REPORT_FILE_NAME + '" en su unidad Google Drive: \n\r' + docReport.getUrl());   
    SpreadsheetApp.flush();
    SpreadsheetApp.getUi().alert('Se ha creado un nuevo informe "' + REPORT_FILE_NAME + '" en su unidad Google Drive \r\n');
  } catch(e) {
    Logger.log("ERROR in function createPdf \r\nMessage: " + e.message + "\r\nFile gs: " + e.fileName + "\r\nLine: " + e.lineNumber)
    Logger.log("\r\nUser: " + Session.getActiveUser().getEmail() + ", Date: " + Utilities.formatDate(new Date(), "GMT+1", "yyyyMMdd'_'HH:mm:ss"));
    MailApp.sendEmail(EMAIL_DEV, SUBJECT_MAIL, Logger.getLog());   
  } 
}

3 个答案:

答案 0 :(得分:0)

不可能电子表格过滤器是特定的&#34;视图&#34;无法在应用脚本中检索到的内容。但是,如果您知道过滤器视图的参数,则可以手动重建过滤器视图。使用getDataRange()。getValues()来检索所有数据,然后在此表上应用filter function

答案 1 :(得分:0)

最后,我能够实现我的目标,但这不是通常的解决方案,但它确实有效。在处理工作表之前,我选择了所有行,然后我选择了背景颜色,Google Spreadsheet只将背景颜色设置为已过滤的行。然后,我处理所有工作表但我知道过滤了行,因为我询问它的背景。我希望这个解决方案对您有用并且对您有用。感谢。

function docReport() {
  try {
    splashScreen("Generando informe...");    
    var activeSheet = SpreadsheetApp.getActiveSheet();    
    var numberOfColumns = activeSheet.getLastColumn();
    var numberOfRows = activeSheet.getLastRow(); 
    var activeRow, rowRange;
    var titleRow = activeSheet.getRange(1, 1, 1, numberOfColumns).getValues();

    //Set los fondos a otro color para identificar las líneas a las líneas que tienen filtro
    activeSheet.setActiveSelection("2:" + activeSheet.getLastRow()).setBackground("#fffff9"); 

    var docReport =  DocumentApp.create(REPORT_FILE_NAME);
    var bodyReport = docReport.getBody();
    bodyReport.setAttributes(stylePage);

    for (var row = 2; row <= numberOfRows; row++) { //Empieza en 2 ya que no incluyo la fila de los títulos
      rowRange = activeSheet.getRange(row, 1, 1, numberOfColumns);
      if (rowRange.getBackground() == "#fffff9") { //If it has #fffff9 background, it means that it is a filtered row
        activeSheet.setActiveSelection(row + ":" + row).setBackground("#ffffff"); //Volvemos a dejar los fondos en blanco
        activeRow = rowRange.getValues();
        if (!isEmpty(activeRow[0], numberOfColumns)) {
          var image = UrlFetchApp.fetch(IMG_BBVA);
          bodyReport.appendParagraph("").addPositionedImage(image).setHeight(100).setWidth(98).setLayout(DocumentApp.PositionedLayout.WRAP_TEXT);
          bodyReport.appendParagraph("          S.J.").setAttributes(styleTextBlue); 
          bodyReport.appendParagraph(DATE_REPORT + "\r\n\r\n").setAttributes(styleDate); 

          for (var col = 1; col < numberOfColumns; col++) { //Ignoramos la columna 1 que es el contador
            if (activeRow[0][col] != "") { //Si el valor es vacio no lo imprimimos
              if (col == COLUMN_NORMA)
                bodyReport.appendParagraph(String(activeRow[0][col]).trim()).setAttributes(styleTextNorma); //Incluimos contenido
              else { 
                bodyReport.appendParagraph(titleRow[0][col]).setAttributes(styleTitle); //Incluimos título
                bodyReport.appendParagraph(String(activeRow[0][col]).trim()).setAttributes(styleText); //Incluimos contenido
              }
              bodyReport.appendParagraph("");
            }
          }
          bodyReport.appendPageBreak(); 
        }
      }
    } 
    activeSheet.setActiveSelection("2:" + activeSheet.getLastRow()).setBackground("#ffffff"); //Volvemos a dejar los fondos en blanco menos primera fila
    bodyReport.appendPageBreak();     
    docReport.saveAndClose();
    MailApp.sendEmail(Session.getActiveUser().getEmail(), "Informe " + REPORT_FILE_NAME + " generado", 
                      'Se ha creado un nuevo informe "' + REPORT_FILE_NAME + '" en su unidad Google Drive: \n\r' + docReport.getUrl());   
    SpreadsheetApp.flush();
    SpreadsheetApp.getUi().alert('Se ha creado un nuevo informe "' + REPORT_FILE_NAME + '" en su unidad Google Drive \r\n');
  } catch(e) {
    Logger.log("ERROR in function createPdf \r\nMessage: " + e.message + "\r\nFile gs: " + e.fileName + "\r\nLine: " + e.lineNumber)
    Logger.log("\r\nUser: " + Session.getActiveUser().getEmail() + ", Date: " + Utilities.formatDate(new Date(), "GMT+1", "yyyyMMdd'_'HH:mm:ss"));
    MailApp.sendEmail(EMAIL_DEV, SUBJECT_MAIL, Logger.getLog());   
  } 
}

答案 2 :(得分:0)

在Google Issue Trackerendorsed by Google中发布了一个解决方案:

  

您可以使用新的Google表格高级服务来获取已过滤的内容   行,清除所有过滤器,...有关详细信息,请参阅此博客文章:   https://gsuite-developers.googleblog.com/2017/04/using-google-sheets-filters-in-add-ons.html

     

for(var i = 0; i&lt; sheets.length; i ++){
  if(sheets [i] .properties.sheetId == sheetId){
  var data = sheets [i] .data;
  var rows = data [0] .rowMetadata;
  for(var j = 0; j   if(rows [j] .hiddenByFilter)hiddenRows.push(j);
  }
  }
  }
  return hiddenRows;
  }