2017-12-11 1 views
0

Google 스프레드 시트에서 'xyz'의 모든 인스턴스를 검색하고 모든 'y'를 셀 참조 및 시트 이름으로 바꿉니다.GAS : 셀 주소가있는 모든 시트의 모든 정규식 일치 바꾸기

 
A   B 
1 xA1Mondayz 
2   xB2Mondayz 

그리고 그래서 결과가 이것이다 교체

 
A  B 
1 xyz 
2  xyz 

내가 다음 A1과 B2의 텍스트를 원하는 :

내가 '월요일'라는 시트에이 테이블을 상상해 나머지 시트들도 마찬가지입니다. 내가 비슷한 질문을 찾을 수없는 한 지금까지

...

내 코드는 다음과 같이 보입니다 :

function replaceY() { 
    var spreadsheet = SpreadsheetApp.getActive(); 
    var sheets = spreadsheet.getSheets(); 
    //Do for each sheet 
    for(m = 0; m < sheets.length; m++){ 
    var data = sheets[m].getDataRange().getValues(); 
    for(var i = 0; i < data.length; i++){ 
     for(var n = 0; n < data[0].length; n++){ 
     if(typeof(data[i][n]) == 'string'){ 
      var regex = /.*(y).*/ig; 
      var result = regex.exec(data[i][n]); 
      if(result[1] !== null){ 
      var currentCellAddress = sheets[m].getActiveRange(); 
      // Loop through regex matches and replace every group 
      // with currentCellAddress and sheet name 
      } 
     } 
     } 
    } 
    } 
} 

나는 여기에 내 깊이에서 수 있습니다 실현,하지만 당신이 알고있는 경우 해결책을 찾았다면 기꺼이 그것을 볼 수 있습니다!

업데이트 내가받는 오류 : null의 속성 '1'을 읽을 수 없습니다. 그게 경기를 통해 반복하지 않고.

+1

코드의 문제점은 무엇입니까? 오류가 있습니까? :) – sniperd

+0

@sniperd 예. TypeError : null의 속성 '1'을 읽을 수 없습니다. 그게 경기를 통해 반복하지 않고. – Nickelbacker

+0

indexOf ('String') 메서드를 사용하지 않는 이유는 무엇입니까? –

답변

0

코드가 좋습니다. 다음의 추가는 나를 위해 일한 :

function replaceY() { 
    var spreadsheet = SpreadsheetApp.getActive();  // Declaring variables 
    var sheets = spreadsheet.getSheets();    // outside of the loop. 
    var regex = /y/ig;  // Using a simpler regex. 
    var data, name, str, range, result, startRow, startCol, row, col, notation, replaceWith; 

    //Do for each sheet 
    for(m = 0; m < sheets.length; m++){ 
    range = sheets[m].getDataRange() 
    data = range.getValues(); 

    for(var i = 0; i < data.length; i++){ 
     for(var n = 0; n < data[0].length; n++){ 

     if(typeof(data[i][n]) == 'string'){ 
      result = regex.exec(data[i][n]); 

      if(result !== null){ // * I removed the [1] because result was not considered 
           // * an array for some reason. 
      name = sheets[m].getName(); 

      // get the A1 notation, then combine with the sheet name. 
      // create a range with current indices, then get its notation. 
      notation = sheets[m].getRange(i+1, n+1).getA1Notation(); 
      replaceWith = notation+name; 
      data[i][n] = data[i][n].replace(regex, replaceWith); 

      } 
     } 
     } 
    } 
    range.setValues(data); 
    } 
} 

그것은 또한 당신이 수신 된 오류를 방지 코드 (result[1])에서 [1]을 제거하는 것 같다.

나에게도 도움이 될지 알려주세요.

관련 문제