简体   繁体   English

谷歌工作表中的两个活动工作表基于单元格比较更新错误

[英]two active sheets in google sheets update error based on cell comparison

I'm trying to get this code working but I know I'm missing something.我试图让这段代码工作,但我知道我错过了一些东西。 what the code should do is: to find a specific text from another cell using another sheet reference, so there are two sheets in the google sheet below:'BSR DATA' and 'ENTRY FORM'.代码应该做的是:使用另一个工作表引用从另一个单元格中查找特定文本,因此下面的谷歌工作表中有两个工作表:'BSR DATA'和'ENTRY FORM'。 from ENTRY FORM,cell g6 will compare its data to the first column of BSR DATA and if it meets the criteria, it will update the specific row of the array/cell reference with "new value".从 ENTRY FORM 开始,单元格 g6 会将其数据与 BSR DATA 的第一列进行比较,如果符合条件,它将使用“新值”更新数组/单元格引用的特定行。

link to google sheets: https://docs.google.com/spreadsheets/d/1kBuczydffPFBEfy2oegC1WsKXMjSxbGNRzWLCKurOMs/edit?usp=drivesdk链接到谷歌表: https://docs.google.com/spreadsheets/d/1kBuczydffPFBEfy2oegC1WsKXMjSxbGNRzWLCKurOMs/edit?usp=drivesdk

function UpdateBSRSpecial()
{
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var testForm = ss.getSheetByName("ENTRY FORM");
    var testTable = ss.getSheetByName("BSR DATA");
    var testFormValue = testForm.getRange("G6").getValue();
    var rangeData = testTable.getDataRange();
    var lastColumn = rangeData.getLastColumn();
    var lastRow = rangeData.getLastRow();
  for(var i=0;i>lastRow;i++){
     var dataID = testForm.getRange(i,1).getValue();
     if(testFormValue == dataID)
    {
        testTable.getRange(i,6).setValue("new value");
    };
  };
};

there is a code that has been mis-placed, kindly check my code below:有一个代码放错了,请检查下面的代码:

   function UpdateBSRSpecial()
    {
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        var testForm = ss.getSheetByName("ENTRY FORM");
        var testTable = ss.getSheetByName("BSR DATA");
        var testFormValue = testForm.getRange("G6").getValue();
        var rangeData = testTable.getDataRange();
        var lastColumn = rangeData.getLastColumn();
        var lastRow = rangeData.getLastRow();
       for(var i=2;i<=lastRow;i++){
           var dataID = testTable.getRange(i,1).getValue();
          if(testFormValue == dataID)
        {
            testTable.getRange(i,6).setValue("new value");
        };
      }; 
    }; 

hope this helps希望这可以帮助

The existing script is updating all rows because it loops with for(var i=0;i>lastRow;i++){ .现有脚本正在更新所有行,因为它使用for(var i=0;i>lastRow;i++){循环。 The following script demonstrates the process to search for a discrete term.以下脚本演示了搜索离散术语的过程。

I am not going to deal with the updating of the "new value" since that is not clear from the question.我不打算处理“新价值”的更新,因为这在问题中并不清楚。

Aspects to note需要注意的方面

  • the script gets all the data on BSR - var bsrdata = bsr.getRange(1,1,bsrLR,bstLC).getValues();该脚本获取 BSR 上的所有数据 - var bsrdata = bsr.getRange(1,1,bsrLR,bstLC).getValues(); . . This can be used later when updating values.这可以在稍后更新值时使用。
  • using the Javascript map method, Column A is extracted as a separate array for the search - var bsrColA = bsrdata.map(function(e){return e[0];});//[[e],[e],[e]]=>[e,e,e]使用 Javascript map方法,将 A 列提取为单独的数组进行搜索 - var bsrColA = bsrdata.map(function(e){return e[0];});//[[e],[e],[e]]=>[e,e,e]
  • the search is conducted using the Javascript indexOf method.使用 Javascript indexOf方法进行搜索。 Note: if a match is found, the method returns the index number of the search term within the array;注意:如果找到匹配项,则该方法返回数组中搜索词的索引号; if no match is found, the method returns "-1".如果未找到匹配项,则该方法返回“-1”。
  • the script tests to see if the search was successful: if (result !=-1){脚本测试搜索是否成功: if (result !=-1){
  • indexOf returns a zero-based index, therefore the actual row number is the index plus one: var row = result+1; indexOf返回一个从零开始的索引,因此实际的行号是索引加一: var row = result+1;

function so5866895401() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var formsheetname = "ENTRY FORM";
  var form = ss.getSheetByName(formsheetname);
  var bsrname = "BSR DATA";
  var bsr = ss.getSheetByName(bsrname);

  // get input cell on form
  var searchterm = form.getRange("G6").getValue();
  // Logger.log(searchterm);// DEBUG

  //get data from BSR
  var bsrLR = bsr.getLastRow();
  var bstLC = bsr.getLastColumn();
  var bsrdata = bsr.getRange(1,1,bsrLR,bstLC).getValues();

  // get column A of BSR
  var bsrColA = bsrdata.map(function(e){return e[0];});//[[e],[e],[e]]=>[e,e,e]
  // Logger.log(bsrColA);// DEBUG

  // search for the searchterm in ColumnA of BSR
  var result = bsrColA.indexOf(searchterm); // zero-based
  if (result !=-1){
    var row = result+1;
    Logger.log("result = "+result+", row = "+row);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM