如果其他单元格内容符合条件,则返回特定单元格上的值的脚本 [英] Script to return a value on a specific cell if other cell content meets criteria

查看:813
本文介绍了如果其他单元格内容符合条件,则返回特定单元格上的值的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Google脚本,并且试图创建一个非常基本的脚本来帮助我了解基本知识。

I'm just getting started with Google scripts and I'm trying to create a very basic script to help me understand the fundamentals.

该脚本将在单元格E2中查找数字5,然后返回值为5(如果为true)或10(如果为false)。
我遇到的问题是,如果我在E2上输入 5 ,我不会得到 5 在E4上。在E4上总是说 10

The script would look for the number 5 in cell E2 and then return a value of 5 if true or 10 if false. The issue I'm running into is that if I enter a 5 on E2 I do not get a 5 on E4. it always says 10 on E4.

我尝试将运算符更改为 = === 并在 5 周围加上引号( 5 )以查找文本而不是数字并且无法使其正常工作。

I have tried changing the operator to = and === as well as putting quotes ("5") around the 5 to look for text rather than a number and can't get it to work.

function onEdit()
{

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cell = sheet.getRange(2,5);
var cell2 = sheet.getRange(4,5);


  if (cell == 5) {cell2.setValue(5);}
  else {cell2.setValue(10);}


}


推荐答案

错误在于比较 cell == 5 。它应该是 cell.getValue()== 5

The mistake is in comparison cell == 5. It should be cell.getValue() == 5.

实际上,单元格是抽象表示单元格的对象:通过调用其方法之一,可以更改其值,或将背景设为红色,或设置数据验证等。 getValue 是这些方法之一,它返回单元格中包含的值。

Indeed, cell is an object that abstractly represents the cell: by calling one its methods, you can change its value, or make the background red, or set data validation, etc. getValue is one of these methods, which returns the value contained in the cell.

这篇关于如果其他单元格内容符合条件,则返回特定单元格上的值的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆