根据字符串开头传输输入

Transfer Input Based on What String Starts With

我将以下脚本附加到复选框。单击复选框后,会出现一个框,提示用户输入新的 SKU。然后,这会将 SKU 添加到“order”和“pto”sheets,理想情况下会将其输入 sheet,具体取决于 SKU 的开头数字。

我有以 1、2、3 和 4 开头的 SKU,sheets 100 INV、200 INV、300 INV 和 400 INV 对应于这些 SKU。

这个脚本是有用的,但我用了 4 次,有 4 个单独的复选框。我希望将它简化为一个复选框,该复选框的脚本检测输入的 SKU 以什么数字开头,并相应地插入它。

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
  }
} 

我会想象它是这样的 - 嵌套的 if 语句检查输入的子字符串 - 但无法获取脚本不会 运行:

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
      }
    } 

我对脚本世界还是个新手,非常感谢任何指导!

尝试使用以下脚本:

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;
    }
  }
 }