0

我有大量的稀疏矩陣數據,我希望以編程方式將其設置爲活動單元格(0),當我運行下面的代碼片段(函數)時將其設置爲活動單元格。設置setActiveSelection爲零

這將緩解我的代碼操縱標籤

的數據是完全隨機的。該過程必須從當前活動單元開始,並且如果找到數據的末尾,則必須從頭開始再次循環。

我得到意想不到的效果,

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

這應該找到所有的零的照顧。還添加了一個菜單,可以直接從工作表運行命令。只需將代碼粘貼到腳本編輯器中並重新加載工作表即可。

// 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") 

    } 


} 
+0

謝謝賈根 - 這工作真棒,任何的性能提升可以理解的,因爲我有一個大容量(在許多張),因爲我曾描述 –

+0

什麼是這裏您的最終目標是什麼?激活零單元格後,你會做什麼? –

+0

最終目標是 - 我得到一張具有特定值(座標)和單元格地址的表單,如果在給定單元格地址中找到0,我必須手動刪除零並插入座標。給定的座標是(硬拷貝)。你能告訴我爲了向後移動我需要做的改變 - 有時候光標會出現錯誤。 (你可以簡單地告訴我,我會嘗試自己做) –