0

我试图在谷歌电子表格中计算谷歌应用程序脚本中的一些变量。这不像我想的那样工作。代码是:

for(var n in namen) {
      var naam = namen[n];
      var nr = n;
      if(w == 1) {
        var nr = 3+n;
      } if(w == 2) {
        var nr = 17+n;
      } if(w == 3) {
        var nr = 31+n;
      } if(w == 4) {
        var nr = "45"+n;
      } if(naam == title){
        ssRooster.getRange(nr, col, 1, 1).setValue(dateStr);
        var nr = n;
      }
    }

或者代码是:

  } if(naam == title){
            ssRooster.getRange(n+row, col, 1, 1).setValue(dateStr);
          }

应该没问题,但我现在得到 n 中的数字可以说 2,行中的数字可以说 17。它现在的结果是 217 而不是 19。我该如何解决这个问题?

更喜欢其中包含行的代码。(更干净)

谢谢丹尼斯

[编辑] 完整的代码是:

function LoadWorkTime() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var Period = Browser.inputBox("Periode","Welke periode wilt U zien. (kies tussen de 1 en de 12.)", Browser.Buttons.OK);
  var ErrorPeriod = "De periode die U invoert is onjuist!";
  if(Period>12){
    Browser.msgBox(ErrorPeriod);
    return;
  } if(Period<1){
    Browser.msgBox(ErrorPeriod);
    return;
  }
  var ssPeriode = ss.setActiveSheet(ss.getSheets()[1]);
  var ThisYear = ssPeriode.getRange(2, 1, 1, 1).getValue();
  var CheckYear = Browser.msgBox("Jaar","Is het jaar " + ThisYear + " het jaar dat U wilt opvragen.", Browser.Buttons.YES_NO);
  if(CheckYear=="yes") {
    var CheckYear = ThisYear;
  } else {
    var PastYear = ThisYear-1;
    var AfterYear = ThisYear-0+1;
    var CheckYear = Browser.inputBox("Jaar", "Vul in jaar tal in tussen " + PastYear + " en " + AfterYear, Browser.Buttons.OK);
    var ErrorYear = "Het jaar wat U heeft ingevuld is onjuist!";
    if(CheckYear>PastYear){
      Browser.msgBox(ErrorYear);
      return;
    } if(CheckYear<AfterYear){
      Browser.msgBox(ErrorYear);
      return;
    }
  }
  ssPeriode.getRange(1, 1, 1, 1).setValue(Period);
  ssPeriode.getRange(3, 1, 1, 1).setValue(CheckYear);
  var ssRooster = ss.setActiveSheet(ss.getSheets()[0]);
  var calRooster = CalendarApp.getCalendarById("0000@group.calendar.google.com");
  var calVakantie = CalendarApp.getCalendarById("0000@group.calendar.google.com");
  var dateRow = 2
  var row = 3;
  for(var w = 1; w <= 4; ++w) {
    var col = 2;
    var namen = ssRooster.getRange(3, 1, 10, 1).getValues();
    for(var d = 1; d <= 7; ++d) {
      var eventdate = ssPeriode.getRange(dateRow, 2, 1, 1).getValue();
      var eventsRooster = calRooster.getEventsForDay(new Date(eventdate));
      var dateRow = dateRow+1;
      for(var e in eventsRooster) {
        var event = eventsRooster[e];
        var title = event.getTitle();
        var dateStr = event.getStartTime();
        Browser.msgBox(title);
        for(var n in namen) {
          var naam = namen[n];
          Browser.msgBox(naam);
        } 
        if(naam == title){
          ssPeriode.getRange(5, 1, 1, 1).setFormula('=' + n + '+' + row);
          var nr = ssPeriode.getRange(5, 1, 1, 1).getValue();
          ssRooster.getRange(nr, col, 1, 1).setValue(dateStr);
        }
      }
      var col = col+2;
    }
    var row = row+14;
  }
}

我不能做这个工作!在我看来,这是一个很好的代码。要亲自尝试,请运行此安装代码并更改您拥有的日历地址。

   function Install() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  var ssPeriode = ss.setActiveSheet(ss.getSheets()[1]);
  var Wt = 1;
  ssPeriode
  ssPeriode.deleteColumns(2, ssPeriode.getMaxColumns()-1);
  ssPeriode.deleteRows(2, ssPeriode.getMaxRows()-1);
  ssPeriode.clear();
  ssPeriode.clearContents();
  ssPeriode.clearFormats();
  ssPeriode.insertColumnsAfter(1, 1);
  ssPeriode.insertRows(1, 35);
  ssPeriode.getRange(1, 1, 4, 1).setValues([["1"], [""], ["2012"], ["3-1-2000"]]);
  ssPeriode.getRange(2, 1, 1, 1).setFormula('=TEXT(NOW(); "yyyy")');
  ssPeriode.getRange(1, 2, 1, 1).setFormula('=A1');
  ssPeriode.getRange(2, 2, 1, 1).setFormula('=(A3-2000)*364+((B1-1)*28)+A4');
  for(var i = 3; i <= 29; ++i) {
    var c = i-1;
    ssPeriode.getRange(i, 2, 1, 1).setFormula('=B' + c + '+1');
  }
  var c = 2;
  for(var i = i+1; i <= 34; ++i) {
    ssPeriode.getRange(i, 1, 1, 1).setFormula('=SPLIT(TEXT(B' + c + ';"yyyy-ww");"-")');
    var c = c+7;
  }

  var ssRooster = ss.setActiveSheet(ss.getSheets()[0]);
  var Wt = 1;
  var week = 31;
  var dag = 2;
  var nm = 3;
  ssRooster.deleteColumns(2, ssRooster.getMaxColumns()-1);
  ssRooster.deleteRows(2, ssRooster.getMaxRows()-1);
  ssRooster.clear();
  ssRooster.clearContents();
  ssRooster.clearFormats();
  ssRooster.insertColumnsAfter(1, 14);
  ssRooster.insertRows(1, 56);
  for(var col = 1; col <= ssRooster.getMaxColumns(); ++col) {
    ssRooster.setColumnWidth(col, 60);
    if(col == 1) {
      ssRooster.setColumnWidth(col, 80);
    }
  }
  for(var i = 1; i <= 4; ++i) {
    ssRooster.getRange(Wt, 1, 13, 15).setBorder(true, true, true, true, false, false);
    ssRooster.getRange(Wt, 1, 2, 15).setBorder(true, true, true, true, false, false);
    ssRooster.getRange(Wt+2, 1, 11).setNumberFormat("H:mm")
    ssRooster.getRange(Wt, 1, 1, 1).setFormula('=JOIN(P1;"Week ";P1;Periode!B' + week + ')');
    var week = week+1;
    var col = 2;
    for(var j = 1; j <= 7; ++j) {
      ssRooster.getRange(Wt, col, 2, 2).setBorder(true, true, true, true, false, false);
      ssRooster.getRange(Wt+2, col, 11, 2).setBorder(true, true, true, true, false, false);
      ssRooster.getRange(Wt, col, 1, 2).merge();
      ssRooster.getRange(Wt, col, 1, 1).setFormula('=CHOOSE(WEEKDAY(Periode!B' + dag + ';2);"Maandag ";"Dinsdag ";"Woensdag ";"Donderdag ";"Vrijdag ";"Zaterdag ";"Zondag ")&DAY(Periode!B' + dag + ')&CHOOSE(MONTH(Periode!B' + dag + ');" jan";" feb";" mrt";" apr";" mei";" jun";" jul";" aug";" sep";" okt";" nov";" dec")');
      var dag = dag+1;
      var col = col+2;
    }
    var Wt = Wt+1;
    ssRooster.getRange(Wt, 1, 1, 15).setValues([["Naam", "van", "tot", "van", "tot", "van", "tot", "van", "tot", "van", "tot", "van", "tot", "van", "tot"]]);
    for(var k = 1; k <= 6; ++k) {
      var Wt = Wt+1;
      ssRooster.getRange(Wt, 1, 1, 15).setBackground('yellow');
      var Wt = Wt+1;
    }
    var Wt = Wt-12;
    if( i == 1) {
      var Wt = Wt+13;
    }
    if( i >= 2) {
      for(var k = 1; k <= 11; ++k) {
        var Wt = Wt+1;
        ssRooster.getRange(Wt, 1, 1, 1).setFormula('=A' + nm);
        var nm = nm+1;

      }
      var Wt = Wt+1;
      ssRooster.getRange(Wt, 1, 1, 15).setBorder(true, false, true, false, false, false);
      var Wt = Wt+1;
      var nm = nm+3;
    }
  }
  ssRooster.getRange(Wt, 1, 1, 15).clearFormat();
}
4

3 回答 3

1

您还可以使用var nr = new Number(n);来确保 nr 是一个整数。

于 2012-05-11T14:45:05.077 回答
0

I am not sure if I understand but I think the solution is to insure nr is an integer:

var nr = parseInt(n);

and do not use quotes in var nr = "45"+n;

于 2012-05-10T15:30:01.967 回答
0

我现在将数字写入一个单元格,让第三个单元格计数并调用它。代码现在已经启动并运行,旧代码需要一些修改才能让它工作。

现在一切正常。

于 2012-05-12T12:17:46.830 回答