【问题标题】:How to count the number of Cells Selected?如何计算所选单元格的数量?
【发布时间】:2020-10-15 11:50:15
【问题描述】:

我正在尝试将此宏传递给 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`

主要思想是选择一个通用的单元格范围,脚本会告诉我你选择了多少个单元格,如果是某个工作表,则给我这个数字除以 2,如果是其他工作表,则除以 4,

这是我的第一次尝试,但它不起作用。

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

提前致谢。 哈维尔

【问题讨论】:

  • 是的,我试过了,但我没有找到信息,这就是我的原因,谢谢你的帮助
  • 您是否与console.log() 核对过每一步返回的内容? number_of_cells_selected返回一个范围。以下是Range 的操作
  • 是的,我已经检查过了,似乎我没有很好地定义变量,我已经更改了代码,它没有返回我的范围。

标签: javascript excel google-sheets


【解决方案1】:

我终于找到了解决这个问题的方法,虽然粗鲁,但它确实有效。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多