0

使用下面的代码,我可以查看电子表格中的多个工作表,以找到等于所选单元格的第一个值。该位的唯一问题是:找到值的单元格以黄色突出显示,但未选择找到值的单元格。请参阅下面的代码以浏览工作表。我无法理解这个:)

有趣的是,当我不浏览工作表列表时,突出显示和选择值的代码确实有效,请参阅最佳答案:使用谷歌脚本在电子表格中查找值

function SearchAndFind() {

//determine value of selected cell
var sh = SpreadsheetApp.getActiveSpreadsheet();
var ss = sh.getActiveSheet();
var cell = ss.getActiveCell();
var value = cell.getValue();

//create array with sheets in active spreadsheet
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();

//loop through sheets to look for value
for (var i in sheets) {

 //Set active cell to A1 on each sheet to start looking from there
  SpreadsheetApp.setActiveSheet(sheets[i])
  var sheet = sh.getActiveSheet();
  var range = sheet.getRange("A1");
  sheet.setActiveRange(range);

//set variables to loop through data on each sheet
  var activeR = cell.getRow()-1;
  var activeC = cell.getColumn()-1;
  var data = sheets[i].getDataRange().getValues()
  var step = 0

//loop through data on the sheet  
  for(var r=activeR;r<data.length;++r){
    for(var c=activeC;c<data[0].length;++c){
      step++
      Logger.log(step+' -- '+value+'  =  '+data[r][c]);
      if(data[r][c]==''||step==1){ continue };
      if(value.toString().toLowerCase()==data[r][c].toString().toLowerCase()){
         sheet.getRange(r+1,c+1).activate().setBackground('#ffff55');
        return;
      }
    }
  }
 }

}
4

1 回答 1

0

此代码能够跨多个工作表进行搜索,它显然基于您发布的代码,但使用内存(scriptProperties)在从一张工作表更改为下一张工作表时保持搜索值“活动”并知道何时搜索它. 它有 2 个非最佳方面: 1° 您必须一直搜索到最后一次出现,然后才能开始新的搜索。2°:当它从工作表 n 切换到工作表 n+1 时,它首先选择单元格 A1,然后再找到值出现。

我想应该有可能摆脱这些问题,但现在我不知道如何:-) 也许这种方法根本不是最好的,我从一个简单的单页脚本开始修改和复杂化......这通常不是最好的开发策略(我知道),但无论如何,这是一个有趣的实验和一个很好的逻辑练习......谢谢你。

function SearchAndFind() {

  //determine value of selected cell
  var sh = SpreadsheetApp.getActiveSpreadsheet();
  var ss = sh.getActiveSheet();
  var cell = ss.getActiveCell();
  var value = cell.getValue();
  if(ScriptProperties.getProperty('valueToFind')!=''){value = ScriptProperties.getProperty('valueToFind')};

  //create array with sheets in active spreadsheet
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets()
  var sheetNumber = sheets.length;
  var currentSheet = ss.getIndex()-1;
  Logger.log(currentSheet);

  //loop through sheets to look for value
  for (var i = currentSheet ; i<sheetNumber ; ++i ){
    Logger.log('currentSheet = '+i)
    //Set active cell to A1 on each sheet to start looking from there
    SpreadsheetApp.setActiveSheet(sheets[i])
//    sheets[i].getRange(1,1).activate();
    //set variables to loop through data on each sheet
    var activeR = cell.getRow()-1;
    var activeC = cell.getColumn()-1;
    var data = sheets[i].getDataRange().getValues()
    var step = 0;

    //loop through data on sheet  
    for(var r=activeR;r<data.length;++r){
      for(var c=activeC;c<data[0].length;++c){
        step++
          Logger.log('sheet : '+i+'    step:'+step+'   value '+value+'  =  '+data[r][c]);
        if(data[r][c]==''||(step==1&&i==currentSheet)){ continue };
        if(value.toString().toLowerCase()==data[r][c].toString().toLowerCase()){
          sheets[i].getRange(r+1,c+1).activate().setBackground('#ffff55');
          ScriptProperties.setProperty('valueToFind',value);
        return;
        }
      }
    }
      cell = sheets[i].getRange(1,1);

  }  
  ScriptProperties.setProperty('valueToFind','');
  Logger.log('reset');
}
于 2013-11-07T11:57:10.280 回答