简体   繁体   English

在Google Apps脚本中使用范围

[英]Working with Ranges in Google Apps Scripts

Im writing a simple function in Google Spreadsheets. 我在Google Spreadsheets中编写了一个简单的函数。 I want to input two ranges in the argument something like this: 我想在参数中输入两个范围,如下所示:

=EctsPartial(C3:C4, E3:E4)

For the following function I wrote: 对于以下功能,我写道:

function EctsPartial(rangeA, rangeB) {

  Logger.log(rangeA+"  "+rangeB);
  var noten = SpreadsheetApp.getActiveSheet().getRange(rangeA).getValues();
  var ects = SpreadsheetApp.getActiveSheet().getRange(rangeB).getValues();

  for(var i=0; i < SpreadsheetApp.getActiveSheet().getRange(rangeB).getHeight(); i++){

      if(noten[i] != "" && noten[i] != 5) {
      summe = summe - 0;
      ects[i] = ects[i] - 0;

      summe = summe + ects[i];
   }
    Logger.log(i+":");
    Logger.log(summe);
   } 

  return summe;  

};

But the program keeps telling me that the argument of getRange() is not correct. 但是程序不断告诉我,getRange()的参数不正确。 If I manually type "C3:C4" (including the ") it works but otherwise it doesn't. What am I doing wrong? 如果我手动键入 “ C3:C4” (包括“”),则可以,但不能,请问我在做什么错?

I think this is what you are trying to do. 我认为这就是您想要做的。 This is for custom spreadsheet functions. 这用于自定义电子表格功能。

In spreadsheet, the following code allows you to type =EctsPartial(C1) instead of =EctsPartial("C1") . 在电子表格中,以下代码允许您键入=EctsPartial(C1)而不是=EctsPartial("C1") If you put return noten on the script, it will get the value of C1 如果将return noten放在脚本上,它将获得C1的值

function EctsPartial(rangeA, rangeB) {
  if (rangeA.map) {
    return rangeA.map(EctsPartial);
  } else {
    var noten = rangeA;
  }
}

https://developers.google.com/apps-script/guides/sheets/functions#optimization https://developers.google.com/apps-script/guides/sheets/functions#optimization

A couple of options include: 几个选项包括:

1. 1。

=EctsPartial("C3:C4"; "E3:E4")

.gs: .GS:

function EctsPartial(rangeA, rangeB) {
  var noten = SpreadsheetApp.getActiveSheet().getRange(rangeA).getValues();
  var ects = SpreadsheetApp.getActiveSheet().getRange(rangeB).getValues();
  var sum = 0;
  noten.forEach(function(value) {
    sum += value[0];
  });
  ects.forEach(function(value) {
    sum += value[0];
  });
  return sum;
}

在此处输入图片说明

2. 2。

=EctsPartial(C3:C4; E3:E4)

.gs: .GS:

function EctsPartial(rangeA, rangeB) {
  var sum = 0;
  rangeA.forEach(function(value) {
    sum += value[0];
  });
  rangeB.forEach(function(value) {
    sum += value[0];
  });
  return sum;
}

在此处输入图片说明

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

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