Checking if a sheet cells includes errors or not

房东的猫 提交于 2020-07-10 07:04:07

问题


I am using this script function to check if my cell functions in google sheet has any errors or not.

Here is the code but it does not seems to be working. It keeps on saying no error when i have an error in a cell

var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sourcename = "Sheet1";
  var source = ss.getSheetByName(sourcename);

  var cell = source.getRange("A1:AG30");

function isError2(cell) { 
  const errorValues = ["#NULL!", "#DIV/0!", "#VALUE!", "#REF!", "#NAME?", "#NUM!", "#N/A","#ERROR!"];
  if (errorValues.includes(cell) != true) {
  Logger.log("no error");
  } else{
    Logger.log("some error");
  }

}

Update

function isError2() {  
var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sourcename = "Sheet1";
      var source = ss.getSheetByName(sourcename);

      var cell = source.getRange("A1:AG30");
      const errorValues = ["#NULL!", "#DIV/0!", "#VALUE!", "#REF!", "#NAME?", "#NUM!", "#N/A","#ERROR!"];
      if (errorValues.includes(cell) != true) {
      Logger.log("no error");
      } else{
        Logger.log("some error");
      }

    }

Update 2.0 Updated the approach but still having no luck with the desired output

var mysheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
 var sheet1 = SpreadsheetApp.setActiveSheet(mysheet);

function findErrors(sheet) {
   const errorValues = ["#NULL!", "#DIV/0!", "#VALUE!", "#REF!", "#NAME?", "#NUM!", "#N/A","#ERROR!"];

  var singleSheetArray = [];

  var name = sheet1.getName();

  // how many cells in the sheet currently
  var maxRows = sheet1.getMaxRows();
  var maxCols = sheet1.getMaxColumns();

  var totalCells = maxRows * maxCols;

  // how many cells have data in them
  var r = sheet1.getLastRow();
  var c = sheet1.getLastColumn();
  var data_counter = r * c;

  if (data_counter !== 0) {

    var dataRange = sheet1.getRange(1,1,r,c);
    var dataValues = dataRange.getValues();

    dataValues.forEach(function(row) {
      row.forEach(function(cell) {
        if ((errorValues.indexOf(cell) === -1) )  {
           SpreadsheetApp.getUi().alert("no errors in "+cell);          
          data_counter --;
        } 

      });
    });  
  }
}


回答1:


Problem

Unable to check whether the cell has an error

Explanation

The issue you are facing is a simple type mismatch. getRange() method returns an instance of Range, while you try to compare it to a member of a errorValues array, which consists of strings. Therefore, errorValues.includes(cell) will always be false, hence first block of the conditional statement executing.

Solution

Use getValues() on the range, it will return you a 2-dimensional array of values. If you are only interested in one row (which you probably are), extract it and loop over the cells with some (or every) method, doing the same comparison.

Notes

  1. On using global variables in custom functions and in GAS in general. You can use them, GAS environment is a JavaScript runtime with a convenience layer that simplifies working with Google APIs, nearly everything that's valid in JS is valid here. That being said, do treat global variables as if they don't exist - unless you know exactly what you are doing.

References

  1. getRange method reference
  2. getValues method reference
  3. Custom functions guide
  4. every method reference on MDN (see some there)



回答2:


Try to move the 4 variables inside your function. Apps script does not support global variables. So the function does not recognize the var cell.

EDIT: Detect formula errors in Google Sheets using Script



来源:https://stackoverflow.com/questions/62221236/checking-if-a-sheet-cells-includes-errors-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!