search spreadsheet column for text in a string and return a result in another column

前端 未结 2 1457
灰色年华
灰色年华 2020-12-31 22:23

Using google apps script and spreadsheet, I have been trying to do a simple thing but can\'t figure out the problem. I have a sheet, with a blank column and a column with te

相关标签:
2条回答
  • 2020-12-31 23:03
    function myFunction() {
      //Variable to keep track of the sheet
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      //Start at row 1, end at the last row of the spreadsheet
      for(var i=1;i<sheet.getLastRow();i++){
        var value = sheet.getRange(i, 1).getValue();
        //Compare the value of the cell to 'xyz', if it is then set column 4 for that row to "Yes"
        if(value == 'xyz'){
          sheet.setActiveRange(sheet.getRange(i, 4)).setValue('Yes');
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-31 23:05

    This is a possible simple script that does what you need. (If your sheet contains formulas or custom function then it should be modified to take it into account)

    function test(){
      var sh = SpreadsheetApp.getActiveSheet();
      var data = sh.getDataRange().getValues(); // read all data in the sheet
      for(n=0;n<data.length;++n){ // iterate row by row and examine data in column A
        if(data[n][0].toString().match('xyz')=='xyz'){
          // if column A contains 'xyz' then set value in index [5] (is column F)
          data[n][5] = 'YES'
        };
      }
      Logger.log(data)
      sh.getRange(1,1,data.length,data[0].length).setValues(data); // write back to the sheet
    }
    
    0 讨论(0)
提交回复
热议问题