循环遍历电子表格中的工作表

时间:2017-06-09 17:30:26

标签: javascript

有人可以告诉我如何在5-45页上运行以下代码吗? 我目前在每个工作表上创建了一个按钮来执行下面的代码 告诉它要做,但我想在表50中制作一个按钮来获取代码 下面在5-45的每张纸上运行。 (我不是很擅长javascript ......只是寻求帮助)

// To import data from the the last sheet and populate it 
// into rows D and E of the current worksheet

function SearchCols() 
{
var ss = SpreadsheetApp.getActiveSpreadsheet();                                               
var expen = ss.getSheetByName("MRS summary")                                                  
var sheet1 = expen.getRange("A13:A210").getValues();                                          

var searchCol1 = expen.getRange ('B13:B210').getValues();                                     
var searchCol2 = expen.getRange ('A13:A210').getValues();                                     
var searchCol3 = expen.getRange('C13:C210').getValues();                                      
var searchCol4 = expen.getRange('D13:D210').getValues();                                         

var sss = SpreadsheetApp.getActiveSpreadsheet();                                              
var searchCol = sss.getRange('A13:A210').getValues();                                         
var searchVal = sss.getRange('B4').getValue();                                                
var col1 = sss.getActiveSheet();                                                              
var col2 = sss.getActiveSheet();

for (var i = 0, len = searchCol2.length; i < len; i++)                                                           
  for (var j = 0, len2 = searchCol1.length; j < len2; j++)                                      
  {
    if (searchCol2[j][0] == searchVal && searchCol1[j][0] == searchCol[i]
[0])                   
    { 
      col1.getRange(i + 13, 4).setValue(searchCol3[j][0])                                        
      col2.getRange(i + 13, 5).setValue(searchCol4[j][0])                                        
    }
}
}

我已经尝试添加外部循环(for all var in allsheets))来遍历每张工作表,但它仍然只能在ACTIVE工作表上工作。但是我可以在日志文件中看到(使用Logger.log(g);)它正在循环遍历g的所有值,这意味着表格)我哪里出错了?见下文:

function SearchCols() 
{
  var ssss = SpreadsheetApp.getActive();
  var allsheets = ssss.getSheets();
  for (var g in allsheets)
     {
       Logger.log(g);
       var sheet = allsheets[g]                                              
       var expen = ssss.getSheetByName("MRS summary")                                                  
       var sheet1 = expen.getRange("A13:A210").getValues();                                          

       var searchCol1 = expen.getRange ('B13:B210').getValues();                                     
       var searchCol2 = expen.getRange ('A13:A210').getValues();                                     
       var searchCol3 = expen.getRange ('C13:C210').getValues();                                      
       var searchCol4 = expen.getRange ('D13:D210').getValues();                                         

       var searchCol = ssss.getRange('A13:A210').getValues();                                         
       var searchVal = ssss.getRange('B4').getValue();                                                
       var col1 = ssss.getActiveSheet();                                                              

       for (var i = 0, len = searchCol2.length; i < len; i++)                                                           
       for (var j = 0, len2 = searchCol1.length; j < len2; j++)                                      
       {
         if (searchCol2[j][0] == searchVal && searchCol1[j][0] == 
             searchCol[i][0])                   
         { 
           col1.getRange(i + 13, 4).setValue(searchCol3[j][0])                                        
           col1.getRange(i + 13, 5).setValue(searchCol4[j][0])                                        
         }
       }
     } 
}

0 个答案:

没有答案