在谷歌表格脚本中按特定值递增

问题描述 投票:0回答:1

我目前正在谷歌表格中编写一个脚本,其中我可以使用按钮将计数增加(2,5,10,15,20)-这在特定单元格中有一个下拉菜单。

每次我从下拉列表中选择一个数字时,总数应显示我选择的计数。

例如:下拉列表当前为 5,当我按下按钮时,它将显示 5,然后是 10,然后是 15,依此类推。

谢谢您的帮助

google-sheets google-apps-script formula increment
1个回答
0
投票

尝试:

function incrementCount() {
  // Get the active spreadsheet and the relevant sheets
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  
  // Define the cell references
  var dropdownCell = sheet.getRange("A1"); // Cell with dropdown
  var totalCell = sheet.getRange("B1"); // Cell to display the total

  // Get the current values
  var incrementValue = dropdownCell.getValue();
  var currentTotal = totalCell.getValue();
  
  // Check if the current total is a number, if not initialize it
  if (isNaN(currentTotal)) {
    currentTotal = 0;
  }
  
  // Increment the total by the selected dropdown value
  var newTotal = currentTotal + incrementValue;
  
  // Set the new total back to the total cell
  totalCell.setValue(newTotal);
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.