有没有办法跨细胞搜索模式?

时间:2015-09-19 02:40:15

标签: google-apps-script google-sheets

例如,我有一行(第1行),如:

0 | 1 | 1 | 0 | 0 | 1

在脚本中,我可以指定类似('1','0',0')并且具有正确范围(此处为C1:E1)返回给我吗?

1 个答案:

答案 0 :(得分:0)

你需要这样的东西:

function findPattern() {
  var aSheet = SpreadsheetApp.getActive().getSheetByName("Form Responses 2");
  var pattern = "100",
      howManyRows = 0,
      howManyColums = 0,
      i=0,
      valuesAsString="",
      startCol=0;

  howManyRows = aSheet.getLastRow();
  howManyColums =  aSheet.getLastColumn();

  for (i=0;i<howManyRows;i+=1) { //Go through every row in the sheet
    var range = aSheet.getRange(i+1, 1, 1, howManyColums);
    valuesAsString = range.getValues().join();
    valuesAsString = valuesAsString.replace(/,/g,"");
    //Logger.log("valuesAsString: " + valuesAsString);
    startCol = valuesAsString.indexOf(pattern);
    Logger.log('startCol: ' + startCol);

    if (startCol!==-1) {
      Logger.log('Pattern found in row: ' + (i + 1) + ' Pattern starts in column: ' + (startCol + 1))
    };
  };
};