简体   繁体   English

Google工作表的脚本,用于基于另一个单元格值(在列范围内)更改单元格值

[英]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.

相关问题 Google表格脚本,返回范围值关闭了3个单元格 - Google sheet script, return range value is 3 cells off 根据其他行中单元格中的值有条件地编辑单元格 - 使用 Google Script 的 Google Sheet - Conditionally edit cells based of the value in cells in other rows - Google Sheet using Google Script 用于根据另一个单元格值为单元格着色的 Google 脚本 - Google script to color a cell based on another cells value Google Sheet Script - 根据包含电子邮件的一系列单元格隐藏选项卡 - Google Sheet Script - Hiding tabs based on a range of cells containing emails 使用脚本根据单元格值将单元格复制到第二张工作表 - Copy cells to second sheet based on cell value using a script Google Spreadsheet根据价值填充其他工作表中的单元格 - Google Spreadsheet Populating Cells in Other Sheet Based on Value Google表格:编写一个按钮,以向一列单元格中添加/减去计算值 - Google Sheet: Program a button to add/minus a calculated value to a column of cells 如何使用谷歌脚本确定一个值是否在一系列单元格中? - How to figure out if a value is in a range of cells using google script? Google Apps 脚本 - 根据一个单元格的值复制一系列单元格 - Google Apps Script - Copy a range of cells, depending on the value of one cell 如何根据另一个 function 的结果设置单元格值(单元格范围)? - How to set cell value (range of cells) based on the result of another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM