Home > Net >  Transfer Input Based on What String Starts With
Transfer Input Based on What String Starts With

Time:05-06

I have the following script attached to a checkbox. When the checkbox is clicked, a box prompts the user to enter a new SKU. This would then add the SKU to "order" and "pto" sheets, and ideally will enter it on a sheet, depending on what number the SKU starts with.

I have SKUs that start with 1, 2, 3 and 4, with sheets 100 INV, 200 INV, 300 INV, and 400 INV corresponding to those SKUs.

This script is functional, but I was using it 4 times with 4 separate checkboxes. I'm hoping to simplify it to a single checkbox, the script of which detects what number the entered SKU starts with, and inserting it accordingly.

function addNewAccessorySKU() {
  const ss = SpreadsheetApp.getActive();
  const ui = SpreadsheetApp.getUi();
  const order = ss.getSheetByName('Order');
  const pto = ss.getSheetByName('Pending TOs');
  const sheet1 = ss.getSheetByName('100 INV');
  const sheet2 = ss.getSheetByName('200 INV');
  const sheet3 = ss.getSheetByName('300 INV');
  const sheet4 = ss.getSheetByName('400 INV');
  const response = ui.prompt('WARNING: \r\n \r\n Ensure the entered SKU is NOT already on the following sheets: \r\n \r\n Ordering \r\n Accessory INV \r\n Pending TOs \r\n \r\n Enter New SKU:', ui.ButtonSet.OK_CANCEL);
  if (response.getSelectedButton() === ui.Button.OK) {
    var text = response.getResponseText();
     uncheckAccessory();
     order.appendRow([text]);
     order.sort(1);
     pto.appendRow([text]);
     pto.sort(1);
     sheet1.appendRow([text]);
     sheet1.sort(1);
     myFunction(); //references the Protection script
  }
} 

I would imagine it something like this - a nested if statement checking the substring of the input - but can't get the script won't run:

function addNewAccessorySKU() {
      const ss = SpreadsheetApp.getActive();
      const ui = SpreadsheetApp.getUi();
      const order = ss.getSheetByName('Order');
      const pto = ss.getSheetByName('Pending TOs');
      const sheet1 = ss.getSheetByName('100 INV');
      const sheet2 = ss.getSheetByName('200 INV');
      const sheet3 = ss.getSheetByName('300 INV');
      const sheet4 = ss.getSheetByName('400 INV');
      const response = ui.prompt('WARNING: \r\n \r\n Ensure the entered SKU is NOT already on the following sheets: \r\n \r\n Ordering \r\n Accessory INV \r\n Pending TOs \r\n \r\n Enter New SKU:', ui.ButtonSet.OK_CANCEL);
      if (response.getSelectedButton() === ui.Button.OK) {
        var text = response.getResponseText();
         if(response.getResponseText.substring(1,2)= "1") {
         uncheckAccessory();
         order.appendRow([text]);
         order.sort(1);
         pto.appendRow([text]);
         pto.sort(1);
         sheet1.appendRow([text]);
         sheet1.sort(1);
         myFunction(); //references the Protection script
      }
    } 

I'm still new to the scripting world so any guidance is appreciated!

CodePudding user response:

Try with the following script:

function addNewAccessorySKU() {
  const ss = SpreadsheetApp.getActive();
  const ui = SpreadsheetApp.getUi();
  const order = ss.getSheetByName('Order');
  const pto = ss.getSheetByName('Pending TOs');
  const sheet1 = ss.getSheetByName('100 INV');
  const sheet2 = ss.getSheetByName('200 INV');
  const sheet3 = ss.getSheetByName('300 INV');
  const sheet4 = ss.getSheetByName('400 INV');
  const response = ui.prompt('WARNING: \r\n \r\n Ensure the entered SKU is NOT already on the following sheets: \r\n \r\n Ordering \r\n Accessory INV \r\n Pending TOs \r\n \r\n Enter New SKU:', ui.ButtonSet.OK_CANCEL);

  if (response.getSelectedButton() === ui.Button.OK) {
    var text = response.getResponseText();
    var subText = text.substring(0,1);
    
    switch(subText){
      case "1":
      uncheckAccessory();
      order.appendRow([text]);
      order.sort(1);
      pto.appendRow([text]);
      pto.sort(1);
      sheet1.appendRow([text]);
      sheet1.sort(1);
      myFunction(); //references the Protection script
      break;

      case "2":
      uncheckAccessory();
      order.appendRow([text]);
      order.sort(1);
      pto.appendRow([text]);
      pto.sort(1);
      sheet2.appendRow([text]);
      sheet2.sort(1);
      myFunction(); //references the Protection script
      break;

      case "3":
      uncheckAccessory();
      order.appendRow([text]);
      order.sort(1);
      pto.appendRow([text]);
      pto.sort(1);
      sheet3.appendRow([text]);
      sheet3.sort(1);
      myFunction(); //references the Protection script
      break;

      case "4":
      uncheckAccessory();
      order.appendRow([text]);
      order.sort(1);
      pto.appendRow([text]);
      pto.sort(1);
      sheet4.appendRow([text]);
      sheet4.sort(1);
      myFunction(); //references the Protection script
      break;
    }
  }
 }
  • Related