Google Script: 通过单元格值进行搜索

huangapple go评论69阅读模式
英文:

Google Script: Searching with a cell value

问题

我想要为客户的一个数值添加+1。

例如:

| G8 | K8 |

| Max Mustermann | 140 |

目标:

| G8 | K8 |

| Max Mustermann | 141 |

我有一个工作正常的脚本,但我想要使用来自单元格(G5)的值(Max Mustermann)。

function TextFinden() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
spreadsheet.getRange("G8:G" + spreadsheet.getLastRow()).createTextFinder("Max Mustermann").matchEntireCell(true).findNext().offset(0, 4).activate();

//value +1
var cell = SpreadsheetApp.getActiveSheet().getActiveCell();
var value = cell.getValue() * 1;
cell.setValue(value+1);
}

目标示例:

createTextFinder("G5")

这样,我可以将名称写入单元格(G5)并使用按钮将客户的值加1。

如有任何建议,将不胜感激,谢谢。

英文:

I want to add +1 to a value from a customer.

As example:

| G8 | K8 |

| Max Mustermann | 140 |

Goal:

| G8 | K8 |

| Max Mustermann | 141 |

I have a Script that works fine, but I would like to use the value (Max Mustermann) from a cell (G5).

function TextFinden() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
spreadsheet.getRange("G8:G" + spreadsheet.getLastRow()).createTextFinder("Max Mustermann").matchEntireCell(true).findNext().offset(0, 4).activate();

//value +1
var cell = SpreadsheetApp.getActiveSheet().getActiveCell();
var value = cell.getValue() * 1;
cell.setValue(value+1);
}

Goal example:

createTextFinder("G5")

So, I can write the Name in a cell (G5) and use a Button to add +1 to the Customer.

Any advice will be appreciated, thanks.

答案1

得分: 1

  • 你想要从单元格 "G5" 中检索一个值。
  • 你想要从单元格 "G8:G" 中搜索值。
  • 当找到该值时,你想要在同一行的 "K" 列中添加 1

在这种情况下,以下示例脚本如何:

示例脚本:

function myFunction() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const value = sheet.getRange("G5").getDisplayValue();
  const range = sheet.getRange("G8:G" + sheet.getLastRow()).createTextFinder(value).matchEntireCell(true).findNext();
  if (!range) return;
  const valueRange = range.offset(0, 4);
  valueRange.setValue(valueRange.getValue() + 1);
}

注意:

  • 如果 "K" 列的值是字符串,请修改 valueRange.setValue(valueRange.getValue() + 1)valueRange.setValue(Number(valueRange.getValue()) + 1)

参考:

英文:

I believe your goal is as follows.

  • You want to retrieve a value from cell "G5".
  • You want to search the value from cells "G8:G".
  • When the value is found, you wan to add 1 to the column "K" of the same row.

In this case, how about the following sample script?

Sample script:

function myFunction() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const value = sheet.getRange("G5").getDisplayValue();
  const range = sheet.getRange("G8:G" + sheet.getLastRow()).createTextFinder(value).matchEntireCell(true).findNext();
  if (!range) return;
  const valueRange = range.offset(0, 4);
  valueRange.setValue(valueRange.getValue() + 1);
}

Note:

  • If the value of column "K" is the string value, please modify valueRange.setValue(valueRange.getValue() + 1) to valueRange.setValue(Number(valueRange.getValue()) + 1)

References:

huangapple
  • 本文由 发表于 2023年2月16日 14:58:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定