根据条件将按钮单击时的单元格值更改为列值

Change cell value on button click to column value based on a criteria

我想通过单击按钮 (D3) 来更改单元格 G2 中的值。正在循环的(A 列)值应满足 B 列中的条件 "yes"。

试试这个:

function makingChange() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Sheet1');
  var rg=sh.getRange(2,1,sh.getLastRow()-1,2);
  var vA=rg.getValues();
  var rg2=sh.getRange('G2');
  var vg2=rg2.getValue();
  var rgi=sh.getRange('H2');
  var vi=rgi.getValue();
  var sObj={nA:[]};
  var startIdx=!vi?0:vi;
  if(startIdx>=(vA.length-1))startIdx=0;
  for(var i=startIdx;i<vA.length;i++) {
    if(!vg2 && vA[i][1].toString().toLowerCase()=='yes'){
      rg2.setValue(vA[i][0]);
      sh.getRange(i+2,1).activate();
      sh.getRange('H2').setValue(i);
      break
    }
    if(vg2 && vA[i][1].toString().toLowerCase()=='yes' && vA[i][0]!=vg2) {
      rg2.setValue(vA[i][0]);
      sh.getRange(i+2,1).activate();
      rgi.setValue(i);
      break;
    }
    if(vg2 && vA[i][1].toString().toLowerCase()=='no' && i==vA.length-1) {
      rg2.setValue('');
      rgi.setValue('');
      makingChange();//Thats right this runs the script over again
    }
  }
}

运行先这样

它会给你一个对话框,上面有一个按钮用于 运行 上面的脚本。然后将循环显示第 2 列中带有“是”的行。

function makeDialog() {
  var html='<input type="button" value="Run" onClick="google.script.run.makingChange();" />';
  var userInterface=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showSidebar(userInterface);
};

电子表格:

OP 的目标是遍历名称列表,每个名称的值为 "yes"。

显然这需要跳过值为 "no" 的名称。在处理列表中的姓氏且该名称的 "select" 值为 "no" 时,存在一个特殊的挑战。在那种情况下,循环应该 "restart" 在列表的顶部。

以下代码解决了这些突发事件中的每一个。

使用下面显示的示例数据;代码应该在 Paul、Dave、Brett 和 Ted 上停止。
如果单元格 G2 中的值为 "Ted",则下一个选择的名称应为 Paul。


示例数据截图


function so5794149403() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('57941494');
  var rg=sh.getRange(2,1,sh.getLastRow()-1,2);
  var vA=rg.getValues();
  var vAnum = vA.length;

  var names = sh.getRange(2,1,sh.getLastRow()-1,1).getValues();
  //flatten the array
  var users = names.map(function (row) { return row[0]; }); 

  // set some variables
  var i=0;
  var failedNTLN = 0; // fail but Not The Last Name
  var modifiedTLN = 0;// fail but The Last Name

  //Loop through names
  for (i;i<names.length;i++){
    //Logger.log("DEBUG:0:i = "+i+" and failedNTLN = "+failedNTLN+" and modifiedTLN = "+modifiedTLN);

    if (failedNTLN ==99){
      // if last name was a failure AND it was not the Last name, then do nothing
    }
    else {

      // get the name in Cell G2
      var g2=sh.getRange("G2").getValue();
      // Logger.log("DEBUG:1 currently selected name = "+g2);

      // get the position of G2 in the list of names
      var Pos = users.indexOf(g2);
      // Logger.log("DEBUG:1 position = "+Pos);

      if (modifiedTLN == 99){ 
        // if last name was a failure AND it was The Last Name
        i=0;
        Logger.log("DEBUG:1: modifiedTLN is "+modifiedTLN+", and i = "+i);
      }
      else{
        // test if this is the last name in the list
        if (Pos+1 == names.length){
          // this is the last name
          i=0;
          failedNTLN = 0;
          modifiedTLN = 0;
          Logger.log("DEBUG:1: Pos is last position. i = "+i)
        }
        else
        {
          // this is not the last name
          i = Pos+1;
          // Logger.log("DEBUG:1: Pos is NOT the last position. set i = "+i)
        }
      }
    }

    // Logger.log("DEBUG:1: settings are: i="+i+", next status = "+vA[i][1].toString().toLowerCase()+", next name = "+vA[i][0]+", g2 = "+g2);

    // logic statement if status = yes, and name isn't = G2
    if(vA[i][1].toString().toLowerCase()=='yes' && vA[i][0]!=g2) {

      // Logger.log("DEBUG:2: IF - outcome success: i="+i+", status = "+vA[i][1].toString().toLowerCase()+", name = "+vA[i][0]+" does not equal "+g2);
      sh.getRange("G2").setValue(vA[i][0]);
      // Logger.log("DEBUG:2: setting G2 to "+vA[i][0]+", i = "+i+" and break") 
      failedNTLN=0;modifiedTLN=0;
      break;
    }
    else if (users.indexOf(vA[i][0]) == (names.length-1)){
      // the next name is the last name
      // Logger.log("DEBUG:3: the next name is the last name");
      modifiedTLN = 99;
      failedNTLN=0;
      i=0;
      // Logger.log("DEBUG:3: position = "+users.indexOf(vA[i][0]))
      // Logger.log("DEBUG:3: the next name: "+users.indexOf(vA[i][0])+" is the last name;  i = "+i+"; set modifiedTLN to "+modifiedTLN+"; set failedNTLN to "+failedNTLN);
    }
    else{
      // the next name is NOT the last name
       failedNTLN = 99;
       modifiedTLN = 0;
       //Logger.log("DEBUG:4: the next name: "+users.indexOf(vA[i][0])+" is NOT the last name;  i = "+i+"; set failedNTLN to "+failedNTLN+"; set modifiedTLN to "+modifiedTLN);
    }
  }
}