Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何计算所选单元格的数量?_Javascript_Excel_Google Sheets - Fatal编程技术网

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

Javascript 如何计算所选单元格的数量?,javascript,excel,google-sheets,Javascript,Excel,Google Sheets,我正在尝试将此宏传递给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" &am

我正在尝试将此宏传递给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
  
  }
  
}
提前谢谢。
哈维尔

我终于找到了这个问题的解决方案,但它确实有效

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

是的,我试过了,但是我找不到信息,这就是为什么我是赫拉,谢谢你的帮助。你检查了
console.log()
每一步都返回了什么?所选的
单元格数\u将返回一个范围。这里是我检查过的带有Yes的操作,似乎我没有很好地定义变量,我更改了代码,它没有返回范围。