[英]Script for Google sheet to change a cells value based on another cells value (in a column range)
Here is the scenario: Column E in my Googlesheet has a dropdown list of Yes and No. Everytime the user answers No, I want the corresponding cell in Column G to have the words "Not Applicable". 情况如下:我的Googlesheet中的E列有一个Yes和No下拉列表。每次用户回答No时,我都希望G列中的相应单元格带有“不适用”字样。 But if user answers Yes, I want that cell in G to have another dropdown list of Yes and No.
但是,如果用户回答“是”,我希望G中的该单元格具有另一个“是”和“否”下拉列表。
I tried to build on the script that I got from this thread: Google Sheets formula to change a cells value based on another cells value 我试图建立在从该线程获得的脚本的基础上: Google表格公式可基于另一个单元格值更改单元格值
It's almost perfect, but I can't make it to work for a range (an entire column, preferrably). 它几乎是完美的,但我无法使其适用于某个范围(最好是整列)。 Any advice would be appreciated :)
任何意见,将不胜感激 :)
Since I wasn't getting any success trying to tweak it for my own use, here's a copy code from the mentioned thread: 由于尝试自行调整并没有取得任何成功,因此以下是上述线程的复制代码:
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var s=ss.getActiveSheet()
var nota=e.range.getA1Notation()
if(nota=="E10"){
var val=e.range.getValue()
if(val=="Levy"){
s.getRange("E11").setDataValidation(null)
s.getRange("E11").setValue("Monthly")
}
else{
s.getRange('E11').clearContent()
var cell = s.getRange('E11');
var rule = SpreadsheetApp.newDataValidation().requireValueInList(['Triannual', 'Quarterly']).build();
cell.setDataValidation(rule);
}}}
And here's my dumb attempt: 这是我的愚蠢尝试:
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var s=ss.getActiveSheet()
var nota=e.range.getA1Notation()
if(nota=="E2:E500"){
var val=e.range.getValue()
if(val=="No"){
s.getRange("G2:G500").setDataValidation(null)
s.getRange("G2:G500").setValue("Not Applicable")
}
else{
s.getRange('G2:G500').clearContent()
var cell = s.getRange('G2:G500');
var rule = SpreadsheetApp.newDataValidation().requireValueInList(['Yes','No']).build();
cell.setDataValidation(rule);
}}}
You need to loop a range of cells, which is why the original script isn't working. 您需要循环一定范围的单元格,这就是原始脚本无法正常工作的原因。
.getValue()
returns a single cell, you can't have a range (as in your edited script). .getValue()
返回单个单元格,您不能有范围(如在编辑的脚本中一样)。
This script will look at the entire page and loop it each time. 该脚本将查看整个页面并每次循环。 This is preferable because you don't have to keep data in order.
这是可取的,因为您不必按顺序保留数据。 In other words, you can jump around Column E and mark things "Yes" or "No" as they come up.
换句话说,您可以在E列周围跳转,并在出现时将其标记为“是”或“否”。 Blank cells are ignored.
空白单元格将被忽略。
function addValidation() {
// Get the spreadsheet and active sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// Get the data as an Array you can iterate and manipulate
var data = sheet.getDataRange().getValues();
// Store a rule to use for the Data Validation to be added if ColE == "Yes"
var rule = SpreadsheetApp.newDataValidation().requireValueInList(["Yes", "No"]).build();
// Loop the sheet
for(var i=0; i<data.length; i++) {
// Test ColE. Note that the array is 0-indexed, so A=0, B=2, etc...
// To change which columns you're testing, change the second value.
if(data[i][4] == "Yes") {
// If it's "Yes," add the Data Validation rule to Col G for that row.
// Note that .getRange() is _not_ 0 indexed, which is why you need `i+1` to get the correct row
sheet.getRange(i+1, 7).clear().setDataValidation(rule);
// If ColE == "No," mark ColG as "Not Applicable"
} else if(data[i][4] == "No") {
sheet.getRange(i+1, 7).clearDataValidations().setValue("Not Applicable");
}
}
}
Also note that this will change values as you change Col E. So, if you change a "Yes" to a "No," Col G will be changed to "Not Applicable." 另请注意,这将在更改Col E时更改值。因此,如果将“是”更改为“否”,则Col G将更改为“不适用”。
for (var i = 0; i < 5000; i++) {
function checkData() {
if(SpreadsheetApp.getActiveSheet().getRange("E" + i).getValue() == "No"){
SpreadsheetApp.getActiveSheet().getRange("G" + i).setValue('Not Applicable');
}
else{
SpreadsheetApp.getActiveSheet().getRange("G" + i).setValue('Applicable');
}
}
}
Hope this works for you:) Btw getting the range of 5000 rows will be VERY slow! 希望这对您有用:)顺便说一句,获得5000行的范围将非常慢! Here is another way you can do it faster!
这是您可以更快地完成操作的另一种方法!
var ss = SpreadsheetApp.getActiveSpreadsheet();
var tsSheet = ss.getSheetByName("YOUR SHEET NAME AT THE BOTTOM OF THE PAGE");
var tsRows = parseInt(tsSheet.getLastRow());
for (var i = 0; i < tsRows; i++) {
function checkData() {
if(SpreadsheetApp.getActiveSheet().getRange("E" + i).getValue() == "No"){
SpreadsheetApp.getActiveSheet().getRange("G" + i).setValue('Not Applicable');
}
else{
SpreadsheetApp.getActiveSheet().getRange("G" + i).setValue('Applicable');
}
}
}
EDIT: 编辑:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var tsSheet = ss.getSheetByName("YOUR SHEET NAME AT THE BOTTOM OF THE PAGE");
var tsRows = parseInt(tsSheet.getLastRow());
for (var i = 0; i < tsRows + 1; i++) {
function checkData() {
if(SpreadsheetApp.getActiveSheet().getRange("E" + i).getValue() == "No"){
SpreadsheetApp.getActiveSheet().getRange("G" + i).setValue('Not Applicable');
}
else{
SpreadsheetApp.getActiveSheet().getRange("G" + i).setValue('Applicable');
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.