Google Apps 脚本生成浮点数而不是整数(但并非总是如此!?!)

Google Apps Script generates float instead of Integer (but not always!?!)

我在采购请求电子表格中有一个自定义脚本。它生成新选项卡并将新选项卡中的链接添加到 'register'(第一个选项卡)以汇总数据。

不幸的是,选项卡名称通常以浮点格式 (206.0) 而不是整数格式生成,因此始终采用整数格式的选项卡名称引用不起作用。我不明白为什么会出现浮动。它不会发生在我们尝试过的每台计算机上,只会发生在大多数计算机上,这使得它更加神秘。

我尝试了不同的方法将其强制转换为整数,但没有找到解决方案。非常欢迎所有建议。

function newPR(e) {
  // First, set up the variables we'll need
  var ui = SpreadsheetApp.getUi()
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();  
  var sheet = sheets[0];
  var templateSheet = sheets[1];
  var highestPurchaseRequest = sheets[3];
  var today = new Date();
  var requestCount = sheet.getRange(6,1).getValue();  //getRange: row, column
  var lastRequestTab = parseInt(highestPurchaseRequest.getName(),10);
  var checkNeeded = ( requestCount != lastRequestTab );
  var loopCount = 0;
  /* Before starting, check whether the highest request number matches the 
     name of the highest estimate tab created so far. If not, try to fix it a few 
     times.  If you can't fix it automatically, flag an error and ask the user to 
     sort it out.
  */
  if( checkNeeded ) {
    while( loopCount < 100 && checkNeeded ) {
      if( requestCount <= lastRequestTab ) {
        requestCount++;
      } else {
        checkNeeded = false;
      }
      loopCount++
    }
    if( checkNeeded ){
      ui.alert("Something went wrong!",'The highest Purchase Request listed on the Summary tab is lower than leftmost Purchase Request tab in the spreadsheet.\n\nI\'ve tried to fix this automatically but haven\'t been able to. Maybe someone dragged tabs into a different order accidentally? The order should be: Register, TemplatePO, TemplateCS, Highest-numbered-request.\n\nPlease correct it and retry. ',ui.ButtonSet.OK);
      return;
    }
  }
    /* Everything looks good, so create a new sheet using "templatePO" as the template, 
       increment the request number and use it to name the new sheet; fill in the 
       current date & estimate number at the top 
    */
    requestCount = ++requestCount >> 0;
    ss.insertSheet(requestCount.toString(), 3, {template: templateSheet});
    // Now that it's made, we need another variable to store a reference to the new sheet
    var newSheet = SpreadsheetApp.getActiveSheet();
    newSheet.getRange(5,9).setValue( today );
    
    // Switch to the summary sheet and add links to the new data
    ss.setActiveSheet( sheets[0] );
    ss.insertRowBefore(6);
    var newCells = sheet.getRange(6,1,1,9);
    // Data from: PR# in I2, Date in I5, From in D4, For/Description in D5, Total in D7, sent in K2, received in K11, Notes in K5
    var requestData = [ [ requestCount, "='" + requestCount + "'!I5", "='" + requestCount + "'!D4", "='" + requestCount + "'!D5", "='" + requestCount + "'!K2", "='" + requestCount + "'!K11", "='" + requestCount + "'!D7","",""] ]
    newCells.setFormulas( requestData );
  
    // Switch back to the new sheet & get ready for user input
    ss.setActiveSheet( newSheet );
    newSheet.setActiveSelection("D5");
}

要将数字转换为没有小数部分的字符串,可以使用toFixed(0)

尝试改变

ss.insertSheet(requestCount.toString(), 3, {template: templateSheet});

ss.insertSheet(requestCount.toFixed(0), 3, {template: templateSheet});