根据 Google sheet 中具有数据验证的订单项过滤值

filter values based on line items with data validation in Google sheet

数据集1

P1 Type Size Color Material Length
Kurta Pyjamas No Sizeethnic_1 Colorethnic_1 Materialethnic_3 Lengthethnic_1
Dhotis Typethnic_1 No Colorethnic_2 Materialethnic_2 No
Sherwani No No Colorethnic_2 No Lengthethnic_2
Men Pyjamas Typeethnic_2 No Colorethnic_2 No No
Kurtas No Sizeethnic_2 Colorethnic_1 No Lengthethnic_1
Ethnic Jackets No No Colorethnic_1 No No

数据集2

Typethnic_1 Typeethnic_2 Sizeethnic_1 Sizeethnic_2 Colorethnic_1 Colorethnic_2 Materialethnic_3 Materialethnic_2 Lengthethnic_1 Lengthethnic_2
Mundu Churidar XS XS Beige Green Blended Silk Blend Above Knee Short
Regular Dhoti Regular Pyjama S S Black Grey Cotton Velevt Ankle Length Medium
Patiala M M Blue Maroon Dupion Viscose Rayon
Jodhpuri L L Brown Multi Wool
Harem XL XL Copper Mustard
XXL XXL Cream
3XL 3XL Gold

问题陈述 – 数据集 1 的命名范围作为下拉列表,其中包含创建“命名范围”的数据集 2 中的值。我想根据位于数据集 1 中的列“P1”中的项目导出值。

我想要实现的目标 – 我的目标是根据位于数据集 1 中的列“P1”中的项目导出值,如果这些列中存在“否”作为值(P1、类型、大小, 颜色, Material, 长度) 对于该特定行项目 (Kurta Pyjamas, Dhotis, Sherwani, Men Pyjamas, Kurtas, Ethinic Jackets)。

预期结果:

还可以看到想要输出的小视频:

https://www.loom.com/share/4bc25874003448cc91fc3dc9a69c4a63

选择 Dhoti

我还发布了一个带有数据集的示例 google sheet。

https://docs.google.com/spreadsheets/d/18guAXXjWIMDQilX8Z0Y4_Avogjs2ESMbrZY7Sb9TaxE/edit?usp=sharing

欢迎提出任何想法或解决方案。

使用 onEdit 触发器提供数据验证

function loadObjectsAndCreateProductDropDown() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet0');
  const psh = ss.getSheetByName('Sheet1');
  const [ph, ...prds] = sh.getRange(1, 1, 10, 6).getValues().filter(r => r[0]);
  const [ch, ...chcs] = sh.getRange(11, 1, 10, 10).getValues().filter(r => r.join());
  let pidx = {};
  ph.forEach((h, i) => { pidx[h] = i });
  let prd = { pA: [] };
  prds.forEach(r => {
    if (!prd.hasOwnProperty(r[0])) {
      prd[r[0]] = { type: r[pidx['Type']], size: r[pidx['Size']], color: r[pidx['Color']], material: r[pidx['Material']], length: r[pidx['Length']] };
      prd.pA.push(r[0]);
    }
  });
  let cidx = {};
  let chc = {};
  ch.forEach((h, i) => { cidx[h] = i; chc[h] = [] });
  chcs.forEach(r => {
    r.forEach((c, i) => {
      if (c && c.length > 0) chc[ch[i]].push(c)
    })
  })
  const ps = PropertiesService.getScriptProperties();
  ps.setProperty('product_matrix', JSON.stringify(prd));
  ps.setProperty('product_choices', JSON.stringify(chc));
  Logger.log(ps.getProperty('product_matrix'));
  Logger.log(ps.getProperty('product_choices'));
  psh.getRange('A2').setDataValidation(SpreadsheetApp.newDataValidation().requireValueInList(prd.pA).build());
}

//I chose to use an installable dropdown.  I'm not sure if it's needed.  Its up to you.

function onMyEdit(e) {
  //e.source.toast('entry')
  const sh = e.range.getSheet();
  if (sh.getName() == 'Sheet1' && e.range.columnStart == 1 && e.range.rowStart == 2 && e.value) {
    //e.source.toast('flag1');
    sh.getRange('C2:G2').clearDataValidations();
    let ps = PropertiesService.getScriptProperties();
    let prodObj = JSON.parse(ps.getProperty('product_matrix'));//recovering objects from PropertiesService
    let choiObj = JSON.parse(ps.getProperty('product_choices'));
    let hA = sh.getRange(1, 1, 1, sh.getLastColumn()).getDisplayValues().flat();
    let col = {};
    hA.forEach((h, i) => { col[h.toLowerCase()] = i + 1 });
    ["type", "size", "color", "material", "length"].forEach(c => {
      if (choiObj[prodObj[e.value][c]]) {
        sh.getRange(e.range.rowStart, col[c]).setDataValidation(SpreadsheetApp.newDataValidation().requireValueInList(choiObj[prodObj[e.value][c]]).build());
      }
    })
  }
}

您可以这样尝试,但我猜您想要不同的东西。

function onMyEdit(e) {
  //e.source.toast('entry')
  const sh = e.range.getSheet();
  if (sh.getName() == 'Sheet1' && e.range.columnStart == 1 && e.range.rowStart == 2 && e.value) {
    //e.source.toast('flag1');
    sh.getRange('C2:G2').clearDataValidations();
    let ps = PropertiesService.getScriptProperties();
    let prodObj = JSON.parse(ps.getProperty('product_matrix'));//recovering objects from PropertiesService
    let choiObj = JSON.parse(ps.getProperty('product_choices'));
    let hA = sh.getRange(1, 1, 1, sh.getLastColumn()).getDisplayValues().flat();
    let col = {};
    hA.forEach((h, i) => { col[h.toLowerCase()] = i + 1 });
    ["type", "size", "color", "material", "length"].forEach(c => {
      if (choiObj[prodObj[e.value][c]]) {
        sh.getRange(e.range.rowStart, col[c]).setDataValidation(SpreadsheetApp.newDataValidation().requireValueInList(choiObj[prodObj[e.value][c]]).build()).offset(-1,0).setFontColor('#000000');
      } else {
        sh.getRange(e.range.rowStart, col[c]).offset(-1,0).setFontColor('#ffffff');
      }
    })
  }
}

这个版本实际上也隐藏了列

function onMyEdit(e) {
  //e.source.toast('entry')
  const sh = e.range.getSheet();
  if (sh.getName() == 'Sheet1' && e.range.columnStart == 1 && e.range.rowStart == 2 && e.value) {
    //e.source.toast('flag1');
    sh.getRange('C2:G2').clearDataValidations();
    let ps = PropertiesService.getScriptProperties();
    let prodObj = JSON.parse(ps.getProperty('product_matrix'));//recovering objects from PropertiesService
    let choiObj = JSON.parse(ps.getProperty('product_choices'));
    let hA = sh.getRange(1, 1, 1, sh.getLastColumn()).getDisplayValues().flat();
    let col = {};
    hA.forEach((h, i) => { col[h.toLowerCase()] = i + 1 });
    ["type", "size", "color", "material", "length"].forEach(c => {
      if (choiObj[prodObj[e.value][c]]) {
        sh.getRange(e.range.rowStart, col[c]).setDataValidation(SpreadsheetApp.newDataValidation().requireValueInList(choiObj[prodObj[e.value][c]]).build()).offset(-1,0).setFontColor('#000000');
        sh.showColumns(col[c])
      } else {
        sh.getRange(e.range.rowStart, col[c]).offset(-1,0).setFontColor('#ffffff');
        sh.hideColumns(col[c]);
      }
    })
  }
}

演示:

这是最新版本的样子:

这是我的 Sheet0:

P1 Type Size Color Material Length
Kurta Pyjamas Sizeethnic_1 Colorethnic_1 Materialethnic_3 Lengthethnic_1
Dhotis Typethnic_1 Colorethnic_2 Materialethnic_2
Sherwani Colorethnic_2 Lengthethnic_2
Men Pyjamas Typeethnic_2 Colorethnic_2
Kurtas Sizeethnic_2 Colorethnic_1 Lengthethnic_1
Ethnic Jackets Colorethnic_1
Typethnic_1 Typeethnic_2 Sizeethnic_1 Sizeethnic_2 Colorethnic_1 Colorethnic_2 Materialethnic_3 Materialethnic_2 Lengthethnic_1 Lengthethnic_2
Mundu Churidar XS XS Beige Green Blended Silk Blend Above Knee Short
Regular Dhoti Regular Pyjama S S Black Grey Cotton Velevt Ankle Length Medium
Patiala M M Blue Maroon Dupion Viscose Rayon
Jodhpuri L L Brown Multi Wool
Harem XL XL Copper Mustard
XXL XXL Cream
3XL 3XL Gold