【问题标题】:Google script - In the following code, instead of 'includes', how to do 'not includes'谷歌脚本 - 在下面的代码中,而不是 \'includes\',如何做 \'not includes\'
【发布时间】:2022-10-17 06:49:53
【问题描述】:

Sample sheet

这是我现在拥有的代码,我想做的也是从 C 中引入所有数据,其中文本不包括 col A 中的“250p”。

const sS = SpreadsheetApp.getActiveSpreadsheet()
function grabData() {
  const sheetIn = sS.getSheetByName('data')
  const sheetOut = sS.getSheetByName('Desired Outcome')
  const range = 'A2:B'
  /* Grab all the data from columns A and B and filter it */
  const values = sheetIn.getRange(range).getValues().filter(n => n[0])
  /* Retrieve only the names if it containes 250p */
  /* In format [[a], [b], ...] */
  const parsedValues = values.map((arr) => {
    const [type, name] = arr
    if (type.toLowerCase().includes('250p')) {
      return name.split('\n')
    }
  })
    .filter(n => n)
    .flat()
    .map(n => [n])
  /* Add the values to the Desired Outcome Sheet */
  sheetOut
    .getRange(sheetOut.getLastRow() + 1, 1, parsedValues.length)
    .setValues(parsedValues)
}

我试着做: if (!type.toLowerCase().includes('250p')) { if (type.whenTextDoesNotContain('250p')) {

但在这两种情况下,我都明白,这不是一个功能。

【问题讨论】:

    标签: if-statement google-apps-script google-sheets data-extraction


    【解决方案1】:

    我相信你的目标如下。

    • 您的电子表格包含“A”和“C”列中的值。
    • 您要检查“A”列。当“A”列的值不包含250p 的文本时,您希望将“C”列中的值复制到目标工作表的“A”列。在这种情况下,您希望按 拆分值。

    修改点:

    • 在您的脚本中,为了从“A”和“C”列中检索值,我认为const range = 'A2:B' 应该是const range = 'A2:C' + sheetIn.getLastRow();,并且const [type, name] = arr 也是const [type, , name] = arr
    • 为了检索250p 未包含在“A”列中的行,我将您的if 语句修改为if (!type.toLowerCase().includes('250p')) {

    当这些点反映到你的脚本中时,它变成如下。

    修改后的脚本:

    const sS = SpreadsheetApp.getActiveSpreadsheet();
    function grabData() {
      const sheetIn = sS.getSheetByName('data');
      const sheetOut = sS.getSheetByName('Desired Outcome');
      const range = 'A2:C' + sheetIn.getLastRow();
      const values = sheetIn.getRange(range).getValues();
      const parsedValues = values.map((arr) => {
        const [type, , name] = arr;
        if (!type.toLowerCase().includes('250p')) {
          return name.split('
    ');
        }
      })
        .filter(n => n)
        .flat()
        .map(n => [n]);
      sheetOut
        .getRange(sheetOut.getLastRow() + 1, 1, parsedValues.length)
        .setValues(parsedValues);
    }
    
    • 如果要检索“A”列中包含250p的行,请将if (!type.toLowerCase().includes('250p')) {修改为if (type.toLowerCase().includes('250p')) {

    笔记:

    • 在此修改后的脚本中,使用了您提供的电子表格。因此,当您更改电子表格时,此修改后的脚本可能无法使用。请注意这一点。

    【讨论】:

    • 非常感谢田池。也为您的详细解释。所以 '!'意思是陈述的反义词?
    • @Genny 谢谢你的回复。我很高兴你的问题得到了解决。关于And so '!' means the opposite of a statement?,即逻辑非运算符。关于这个,我以为你可以在developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…看到详细信息
    • 谢谢你的链接。没有多少谷歌搜索将我引导到参考链接:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 2021-11-24
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多