简体   繁体   English

如何计算所选单元格的数量?

[英]How to count the number of Cells Selected?

I am trying to pass this macro to Javascript:我正在尝试将此宏传递给 Javascript:

Sub Contar_Celdas()

y = Selection.Cells.Count
If ActiveSheet.Name = "TELAS DE SACRIFICIO" Then
    Tiempo = y / 4 'Tiempo en horas en telas
    MsgBox "El rango tiene " & y & " celdas" & vbCrLf & "Tiempo celdas seleccionadas: " & Tiempo & " 
Horas"
Else
    Tiempo = y / 2 'Tiempo en horas
    MsgBox "El rango tiene " & y & " celdas" & vbCrLf & "Tiempo celdas seleccionadas: " & Tiempo & " 
Horas"
End If

End Sub`

The main idea is to select a generic range of cells, and the script shows me how many cells you have selected and give me this number divided by 2 if it is a certain sheets or by 4 if it is other sheet,主要思想是选择一个通用范围的单元格,脚本向我显示您选择了多少个单元格,如果是某个工作表,则给我这个数字除以 2,如果是其他工作表,则除以 4,

This is my firts attempent but It isn´t working.这是我的第一次尝试,但它不起作用。

function Contar_Celdas() {
   
  var app= SpreadsheetApp;
  var activeSheet = app.getActiveSpreadsheet().getActiveSheet(); // Returns de active sheet
  var time;  // Declare the variable time
  var activeRange =activeSheet.getActiveRange().length; // Returns de active range selected on the active sheet
  
 
  
  Logger.log(activeSheet); //
  Logger.log(activeRange); //
  
  
  if (activeSheet == 'TELAS DE SACRIFICIO') {  // If the name of the active sheet is igual to " TELAS DE SACRFICIO" 
    
   time = activeRange / 4;// Return the time in the sheet " TELAS DE SACRIFICIO"
    Browser.msgBox('El rango tiene'+  activeRange +  'celdas '+ '\\n' +'Tiempo de celdas seleccionadas:'  +   time); //Shows in a message the range selected and the number of cell divided by 4
  
  
 } else {
   time =  activeRange/2 ;// Return the time in the rest of active sheets that are not "TELAS DE SACRFICIO"
   Browser.msgBox('El rango tiene'+  activeRange +  'celdas '+ '\\n' +'Tiempo de celdas seleccionadas:'  +   time);//Shows in a message the range selected and the number of cell divided by 2
  
  }
  
}

Thanks in advance.提前致谢。 Javier哈维尔

Finally I have the solution to this problem, to rude but it is working.最后我有这个问题的解决方案,粗鲁但它正在工作。

// Count the number of cells selected

function Contar_Celdas() {
   
  var app= SpreadsheetApp;
  var activeSheet = app.getActiveSpreadsheet().getActiveSheet(); // Returns de active sheet
  var time;  // Declare the variable time
  var activeRange_F_Column= activeSheet.getActiveRange().getColumn(); // Give the number of the firts column selected
  var activeRange_L_Column=activeSheet.getActiveRange().getLastColumn(); // Give the last number of the column selected
  var  numbersofColumns =  activeRange_L_Column-activeRange_F_Column+1  // Give the number of columns selected
  var activeSheetName= activeSheet.getName(); // Give you the name of the sheet.
  var activeRange_F_Row= activeSheet.getActiveRange().getRow(); // Give the number of the firts row selected
  var activeRange_L_Row=activeSheet.getActiveRange().getLastRow();// Give the last number of the row selected
  var numbersofRows= activeRange_L_Row- activeRange_F_Row+1; //Give the total rows selected
  var numberofCellsSelected= numbersofRows*numbersofColumns // Give total numbers of cells selected
 

  if (activeSheetName == 'TELAS DE SACRIFICIO') {  // If the name of the active sheet is igual to " TELAS DE SACRFICIO" 
    
   time = numberofCellsSelected / 4;// Return the time in the sheet " TELAS DE SACRIFICIO"
    Browser.msgBox('El rango tiene'+  numberofCellsSelected +  'celdas '+ '\\n' +'Tiempo de celdas seleccionadas:'  +   time  + "horas"); //Shows in a message the range selected and the number of cell divided by 4
  
   } else {
   time =  numberofCellsSelected /2 ;// Return the time in the rest of active sheets that are not "TELAS DE SACRFICIO"
   Browser.msgBox('El rango tiene'+  numberofCellsSelected +  'celdas '+ '\\n' +'Tiempo de celdas seleccionadas:'  +   time + "horas");//Shows in a message the range selected and the number of cell divided by 2
  
  }
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM