How to check if the value exist in google spreadsheet or not using apps script

女生的网名这么多〃 提交于 2020-07-23 07:00:09

问题


How to check if the value is exist in google spreadsheet or not using apps script I want to check if the Sam exist in the entire spreadsheet or not using apps script. If exist I want to perform task...

function doGet(e) {
  return HtmlService.createHtmlOutput("Hi there");
}

function doPost(e) {
  // this is where telegram works
  var data = JSON.parse(e.postData.contents);
  var text = data.message.text;
  var id = data.message.chat.id;

  var userName  = data.message.from.username;

  if(/^#/.test(text)) {
    var sheetName = text.slice(1).split(" ")[0];
    var sheet = SpreadsheetApp.openById(ssId).getSheetByName(sheetName) ? SpreadsheetApp.openById(ssId).getSheetByName(sheetName) : SpreadsheetApp.openById(ssId).insertSheet(sheetName);
    var comment = text.split(" ").slice(1).join(" ");
    sheet.appendRow([userName,new Date(),id,name,comment,answer]);
  }

  //check if user is new in group
  // this gets the range
  var range = SpreadsheetApp.getActiveRange().getValues();  

  var searchString = "marsad01";

  var isSearchStringInRange = range.some( function(row){
    return row[0] === searchString

  });


  if(isSearchStringInRange){
  // do something
        sendMessage(id, answer, name);

  }else{

    sendGreetingMessage(id, answer, name);

  }


}

is there any way how to do this


回答1:


Answer:

You can define a textFinder and run it over your data range.

Code:

function findSam() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];  
  var range = sheet.getDataRange();
  var textFinder = range.createTextFinder('Sam');
  var locations = [];
  
  var occurrences = textFinder.findAll().map(x => x.getA1Notation());
  
  if (occurrences == []) {
    // do something if "Sam" not in sheet 
  }
  else {
    // do stuff with each range: 
  }  
}

This code will:

  • Find all cells that contain "Sam" in the first sheet of the Spreadsheet
  • Append the Range object that contains "Sam" to an array of ranges
  • Map the array of ranges to an array of A1 notations which are all the cells which contain "Sam".

From here you can do what you wish with the ranges. If "Sam" is not in the sheet then occurrences will be an empty array and you can do here what you wish.

I hope this is helpful to you!

References:

  • Class TextFinder | Apps Script | Google Developers



回答2:


Depending on if you want to select the range or just always use the whole A:A column. In the former case, do this:

// this gets the range
var range = SpreadsheetApp.getActiveRange().getValues();

// this is what you are searching for
var searchString = "Sam";

// this is whether what you are searching for exists 
var isSearchStringInRange = range.some( function(row){
    return row[0] === searchString
});

// then you can proceed to do something like
if( isSearchStringInRange ){
  // do something
}


来源:https://stackoverflow.com/questions/61695894/how-to-check-if-the-value-exist-in-google-spreadsheet-or-not-using-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!