Home > OS >  How to create an if statement to insert text into a UI to enter into Google Sheets cell?
How to create an if statement to insert text into a UI to enter into Google Sheets cell?

Time:09-29

In the below code, promptUPDATE, promptUPDATE2, and promptUPDATE3 all output "The user clicked "No" or the dialog's close button" after I press OK. I think there may be something wrong with my if statements but I'm not sure what. The code should only output that message when I close the box or click No.

function TextBox() {
  // SET UI
var ui = SpreadsheetApp.getUi();

// SET HOT & WARM WORKSHEET
  const sheet = SpreadsheetApp.openById('1JZ-v5n5m0_lyaoLgsHDAa78AtYNP5AsUDo2NRpJbwG4').getSheetByName('HOT WARM CLIENTS');
// SORT WORKSHEET BY DATE OF NEXT ACTIVITY
      sheet.sort(9)
var today = Utilities.formatDate(new Date(), "GMT 1", "MM/dd/yy")
var ui = SpreadsheetApp.getUi();
var valuesToCopy = sheet.getRange("D5:J5").getDisplayValues()



var response = ui.alert(valuesToCopy,"Did we do work for this client today?", ui.ButtonSet.YES_NO);
if (response == ui.Button.YES) {
    const variable6 = sheet.getRange("G5").setValue(today);
} else { 
  Logger.log('The user clicked "No" or the dialog\'s close button.');
}



function promptUPDATE3(){

  // Prompt for the value
  var UPDATE =  SpreadsheetApp.getUi().prompt(valuesToCopy "\n\n What did we help this client with today?").getResponseText();

  // Get the sheet that you want store the value in and set the value in the cell B3
 if (response == ui.Button.OK) {
    SpreadsheetApp.getActiveSpreadsheet().getSheetByName("HOT WARM CLIENTS").getRange("PREVIOUS").setValue( UPDATE );  
} else {
  Logger.log('The user clicked "No" or the dialog\'s close button.');
}}
{ 
  promptUPDATE3();
}
function promptUPDATE()
{
  // Prompt for the value
  var UPDATE =  SpreadsheetApp.getUi().prompt(valuesToCopy "\n\n When should we follow up with this client next? (MM/DD/YY)").getResponseText();

  // Get the sheet that you want store the value in and set the value in the cell B3

if (response == ui.Button.OK) {
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("HOT WARM CLIENTS").getRange("I5").setValue( UPDATE );  
} else {
  Logger.log('The user clicked "No" or the dialog\'s close button.');
}}

{ 
  promptUPDATE();
}


function promptUPDATE2()
{
  // Prompt for the value
  var UPDATE =  SpreadsheetApp.getUi().prompt(valuesToCopy "\n\n Next Follow Up Activity?").getResponseText();

  // Get the sheet that you want store the value in and set the value in the cell B3
 if (response == ui.Button.OK) {
    SpreadsheetApp.getActiveSpreadsheet().getSheetByName("HOT WARM CLIENTS").getRange("J5").setValue( UPDATE );  
} else {
  Logger.log('The user clicked "No" or the dialog\'s close button.');
}}

{ 
  promptUPDATE2();

};};

CodePudding user response:

About In the below code, promptUPDATE, promptUPDATE2, and promptUPDATE3 all output "The user clicked "No" or the dialog's close button" after I press OK. I think there may be something wrong with my if statements but I'm not sure what.,

Modification points:

  • In your script, the same value of response of var response = ui.alert(valuesToCopy,"Did we do work for this client today?", ui.ButtonSet.YES_NO); is used with all 4 if (response == ui.Button. And, at var UPDATE =,,,, the button is not set, and response is not changed.
    • I thought that this might be the reason for your issue.
  • sheet and ui can be used with another part in the same function.
  • I thought that in your situation when the message and the range are populated as an array, the script might be simpler.

When these points are reflected in your script, how about the following modification?

Modified script:

function TextBox() {
  var ui = SpreadsheetApp.getUi();
  const sheet = SpreadsheetApp.openById('1JZ-v5n5m0_lyaoLgsHDAa78AtYNP5AsUDo2NRpJbwG4').getSheetByName('HOT WARM CLIENTS');
  sheet.sort(9)
  var today = Utilities.formatDate(new Date(), "GMT 1", "MM/dd/yy")
  var ui = SpreadsheetApp.getUi();
  var valuesToCopy = sheet.getRange("D5:J5").getDisplayValues()
  var response = ui.alert(valuesToCopy, "Did we do work for this client today?", ui.ButtonSet.YES_NO);
  if (response == ui.Button.YES) {
    const variable6 = sheet.getRange("G5").setValue(today);
  } else {
    Logger.log('The user clicked "No" or the dialog\'s close button.');
  }

  // I modified the below script.
  // This is from your showing script.
  var ar = [
    { // promptUPDATE3
      message: valuesToCopy   "\n\n What did we help this client with today?",
      range: "PREVIOUS"
    },
    { // promptUPDATE
      message: valuesToCopy   "\n\n When should we follow up with this client next? (MM/DD/YY)",
      range: "I5"
    },
    { // promptUPDATE2
      message: valuesToCopy   "\n\n Next Follow Up Activity?",
      range: "J5"
    }
  ];
  ar.forEach(({ message, range }) => {
    var res = ui.prompt(message, ui.ButtonSet.OK_CANCEL);
    if (res.getSelectedButton() == ui.Button.OK) {
      sheet.getRange(range).setValue(res.getResponseText());
      SpreadsheetApp.getUi(); // If you want to show the updated situation during the script is run, please use this.
    } else {
      Logger.log('The user clicked "No" or the dialog\'s close button.');
    }
  });
}

References:

  • Related