【问题标题】:filter values based on line items with data validation in Google sheet在 Google 表格中根据带有数据验证的订单项过滤值
【发布时间】:2022-01-27 07:24:09
【问题描述】:

数据集 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, Length) 用于该特定订单项(Kurta Pyjamas、Dhotis、Sherwani、Men Pyjamas、Kurtas、Ethic Jackets)。

预期结果:

您还可以看到所需输出的小视频:

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

选择Dhoti

我还发布了一个带有数据集的示例谷歌表格。

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

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

【问题讨论】:

  • What I am trying to do here - 我没有看到任何努力,您是否忘记在问题中包含您的代码?
  • 我尝试了许多在线可用的代码,但无法在查询或应用脚本中结合数据验证和列选择。
  • 请将表格添加为文本,而不是图像。了解如何here
  • docs.google.com/spreadsheets/d/… 你可以在这里访问表格
  • 我们中的许多人开始拒绝关注您的 google 帐户链接的想法,因为它们会暴露我们的两封电子邮件,而且我还发现共享驱动器的链接很难删除。因此,如果您希望提高获得答案的机会,您可以考虑使用图像和表格来获取数据。

标签: google-apps-script google-sheets phpexcel export-to-excel google-query-language


【解决方案1】:

使用 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

【讨论】:

  • 感谢您的帮助,如果我先通过脚本流程,它应该检查哪些列适用于相应的产品选择,其次删除不适用的列,第三重新排列适用的列并创建验证
  • 我不明白你最后的评论
  • 例如如果我选择 kurtas 类型和材料列不适用,则应删除“不适用”列并仅保留适用的列并进行验证
  • 我选择不那样做,但你可以随心所欲地做。
猜你喜欢
  • 1970-01-01
  • 2013-03-18
  • 2018-09-14
  • 1970-01-01
  • 2022-06-19
  • 1970-01-01
  • 2015-11-29
  • 1970-01-01
  • 2021-05-16
相关资源
最近更新 更多