将setActiveSelection设置为零

时间:2017-02-16 14:49:35

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

我有大量的稀疏矩阵数据,我想以编程方式设置为活动单元格,只要有(0),当我运行下面的代码片段(函数)时。

这会让我轻松操纵代码标签

数据完全是随机的。该过程必须从当前活动单元开始,如果找到数据结束,它必须从头开始循环。

我得到了意想不到的结果,

function getzero() {
    var sheet = SpreadsheetApp.getActiveSheet();
    var range = sheet.getDataRange();
    var Values = range.getValues();

    for (var i = 0; i < Values.length; i++) {
        for (var j = 0; j < Values[i].length; j++) {
            if (Values[i][j] == 0) {
                sheet.setActiveSelection(sheet.getRange(i + 1, j + 1));
                break;
            }
        }
    }
}

sparse matrix

1 个答案:

答案 0 :(得分:1)

这应该注意找到所有的零。还添加了一个菜单,您可以直接从工作表中运行命令。只需在脚本编辑器中粘贴代码并重新加载工作表即可。

// This function addes a menu Zero and submenu getzero to access your function directly from spreadsheet
function onOpen(){
 var ss = SpreadsheetApp.getActive()
 var menu = [{name:"Find Zero",functionName: "getzero"}]
  ss.addMenu("Zeroes", menu)

}



function getzero() {
    var sheet = SpreadsheetApp.getActiveSheet();
    var range = sheet.getDataRange();
    var Values = range.getValues();
    var selectedRange = sheet.getActiveRange()
    // find active cells row and column
    var startRow  = selectedRange.getRow() -1
    var startCol = selectedRange.getColumn()
    var notFoundZero = true

    //Use the active cells row and column to start the loop
    for (var i = startRow; i < Values.length; i++) {
      if (i == startRow){
        var j = startCol
        } 
      else {
        j =0
      }

        for (j ; j < Values[i].length; j++) {
            // Using "===" makes sure the type is also match else blank is considered as zero too.
            if (Values[i][j] === 0) {
                Logger.log("Values Row X Col:" + i + " X " + j)
                //The below line works as well as sheet.setActiveSelection
                sheet.getRange(i + 1, j + 1).activate()
                //Below code escapes the outer loop
                i = Values.length;
                // this boolean is used to runs or stops the next loop
                notFoundZero = false;
                // breaks inner loop
                break;
            }
        }
    }

  if(notFoundZero){
    for (var i = 0; i <= startRow; i++) {
      if (i == startRow){
        var runTill = startCol
      } 
      else {
         runTill = Values[i].length
      }

        for (var j=0 ; j < runTill; j++) {
            if (Values[i][j] === 0) {

                sheet.getRange(i + 1, j + 1).activate()
                // same as above
                i = Values.length;
                //Used to alert if no more zeros found
                notFoundZero = false;
                break;
            }
        }
    }
   }

   if(notFoundZero)
   {
     var ui = SpreadsheetApp.getUi()
     ui.alert("No More zero Found")

   }


}

如果没有找到零,它将在选择后检查单元格值,它将在选择上方检查。但之后它将停止在选择并警告没有找到零,这可以防止失控循环。

试一试,让我知道它是怎么回事。

修改:在代码下面进行反向搜索

代码未经测试,可能有错误。这不是主要问题,因此没有检查错误。

function getzeroRev() {
    var sheet = SpreadsheetApp.getActiveSheet();
    var range = sheet.getDataRange();
    var Values = range.getValues();
    var selectedRange = sheet.getActiveRange()
    // find active cells row and column
    var startRow  = selectedRange.getRow() -1
    var startCol = selectedRange.getColumn()
    var notFoundZero = true

    //Use the active cells row and column to start the loop

    for (var i = startRow; i >=0; i--) {
      if (i == startRow){
        var j = startCol
        } 
      else {
        j =values[i].length
      }

        for (j ; j >=0; j--) {
            // Using "===" makes sure the type is also match else blank is considered as zero too.
            if (Values[i][j] === 0) {
                Logger.log("Values Row X Col:" + i + " X " + j)
                //The below line works as well as sheet.setActiveSelection
                sheet.getRange(i + 1, j + 1).activate()
                //Below code escapes the outer loop
                i = Values.length;
                // this boolean is used to runs or stops the next loop
                notFoundZero = false;
                // breaks inner loop
                break;
            }
        }
    }

  if(notFoundZero){
    for (var i = values.length; i >= startRow; i--) {
      if (i == startRow){
        var runTill = startCol
      } 
      else {
         runTill = 0
      }

        for (var j=0 ; j >= runTill; j--) {
            if (Values[i][j] === 0) {

                sheet.getRange(i + 1, j + 1).activate()
                // same as above
                i = Values.length;
                //Used to alert if no more zeros found
                notFoundZero = false;
                break;
            }
        }
    }
   }

   if(notFoundZero)
   {
     var ui = SpreadsheetApp.getUi()
     ui.alert("No More zero Found")

   }


}