简体   繁体   中英

How to count the number of Cells Selected?

I am trying to pass this macro to 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,

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
  
  }
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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