Google Script Run函数如果在另一个表中的文本列表中包含A'特定文本'

发布于 2025-02-05 17:30:10 字数 922 浏览 2 评论 0原文

我已经对此进行了广泛的搜索,但是它们似乎都没有用。他们都给我一张空白。

sharing?usp = sharing” b。然后做其他一些事情,分裂,修剪等...

当数据中的Col. a中的文本为250p时,我想运行此功能。

因此:if:if(data!a1:a包含文本“ 250p”,然后运行函数提取)。

这是我目前拥有的代码:

//this extract works fine but I just need this to work for only those with value 250 in Col A//

function EXTRACT() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('A1').setFormula('=EXTRACTDATA(DATA!A1:A)');

}

function IF250() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('DATA');
  var range = sheet.getRange('DATA!A1:A');
  var values = range.getValues();

  if (values[i] == "250g") {
    EXTRACT();

更好,如果我可以将数据集中在2个单独的床单中。一张纸上的250年代&一张纸中的500。但这不是必需的。

I've done extensive search for this, but none of them seems to work. They all just give me a blank sheet.
Sample sheet

Basically I have a function that extracts data from Col. B in DATA, to Result. Then does some other things, split, trim etc...

I want to run this function when the text in Col. A in DATA is 250P.

enter image description here

So it would be like: IF (DATA!A1:A contains text "250p" then run function EXTRACT).

This is the code I have as of now:

//this extract works fine but I just need this to work for only those with value 250 in Col A//

function EXTRACT() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('A1').setFormula('=EXTRACTDATA(DATA!A1:A)');

}

function IF250() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('DATA');
  var range = sheet.getRange('DATA!A1:A');
  var values = range.getValues();

  if (values[i] == "250g") {
    EXTRACT();

Better yet, If I can have the data set in 2 separate sheets. The 250s in one sheet & 500s in one sheet. But this is not necessary.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由如风 2025-02-12 17:30:10

查看表格后,这是一个可能的解决方案

code.gs
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)
}

After reviewing your sheet, this is a possible solution

Code.gs
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)
}
删除→记忆 2025-02-12 17:30:10

尝试更改:

var values = range.getValues();

因为

var values = range.getDisplayValues()

这将读取所示的值。尝试使用两者都记录值,以了解原因! (空白)

您当前也没有迭代或循环您的值。

如果您只是想查看该列是否包含一个包含值250p的单元格,请尝试:

function IF250() {

  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(`DATA`)

  const valueExists = sheet.getRange(`A1:A`)
                           .getDisplayValues()
                           .filter(String)
                           .some(row => row.includes(`250P`))

  if (valueExists) EXTRACT()

}

注释:

function IF250() {

  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(`DATA`)

  const valueExists = sheet.getRange(`A1:A`)
                           .getDisplayValues()
                           // Remove empty cells (not strictly necessary)
                           .filter(String)
                           // If the values include a row containing `250p` return true.
                           .some(row => row.includes(`250P`)) 
  // If valueExists returns true:
  if (valueExists) EXTRACT()

}

Try changing:

var values = range.getValues();

to

var values = range.getDisplayValues()

As this will read the value that is shown. Try logging the values with both to see why! (Blank)

You are also not currently iterating, or looping, your values.

If you're just looking to see if the column contains a cell containing the value 250p, try:

function IF250() {

  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(`DATA`)

  const valueExists = sheet.getRange(`A1:A`)
                           .getDisplayValues()
                           .filter(String)
                           .some(row => row.includes(`250P`))

  if (valueExists) EXTRACT()

}

Commented:

function IF250() {

  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(`DATA`)

  const valueExists = sheet.getRange(`A1:A`)
                           .getDisplayValues()
                           // Remove empty cells (not strictly necessary)
                           .filter(String)
                           // If the values include a row containing `250p` return true.
                           .some(row => row.includes(`250P`)) 
  // If valueExists returns true:
  if (valueExists) EXTRACT()

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文