function setParameter()

in gemini/use-cases/sheets-integration/Code.gs [268:307]


function setParameter(name, value = null, min = null, max = null) {
  const ui = SpreadsheetApp.getUi();
  const isNumeric = min != null && max != null;

  let promptText = `Enter ${name}:`;
  if (isNumeric) {
    promptText = `Enter ${name} (between ${min} and ${max}):`;
  }

  let prompt = null;
  if (value != null) {
    prompt = ui.prompt(
      promptText,
      `Current value is ${value}.`,
      ui.ButtonSet.OK_CANCEL,
    );
  } else {
    prompt = ui.prompt(promptText, ui.ButtonSet.OK_CANCEL);
  }

  if (prompt.getSelectedButton() == ui.Button.OK) {
    const inputText = prompt.getResponseText().trim();
    const inputValue = isNumeric ? Number(inputText) : inputText;

    if (
      inputValue != null &&
      (!isNumeric || (inputValue >= min && inputValue <= max))
    ) {
      console.log(`User updated ${name} to ${inputValue}.`);
      return inputValue;
    } else {
      ui.alert("Please enter a valid input.");
      return setParameter(name, value, min, max);
    }
  } else {
    console.log("User canceled the dialog.");
  }

  return null; // Return null if cancelled or input is invalid after retry
}