2017-02-10 14 views
0

Stack Exchangeでの最初の質問ですが、うまくいけば意味があります。複数のGoogle検索での検索と置換のためのGoogle Appsスクリプト

いくつかの背景:私は学校環境で働き、学習支援スタッフが特定の生徒の読みやすいタイムテーブルを作成するのを手伝っています。

私たちのウェブサイトから、科目コード、先生の名前、部屋番号を含むタイムテーブルデータをコピーしています。下の画像に表示されているのとまったく同じ形式です。単にGoogleスプレッドシートにコピーしただけです。

GoogleSheetsImage

は、私は基本的に見つけると、これらすべてのコードに置き換えるバルクを実行し、これらを完全に展開する必要があるように、対象コード例01ENG02は「英語」になり、教師コードなどになります。 JBOは「Joe Bloggs」になります

私はコードを拡張するために必要なものの完全なリストを持っています。ここで

は私がスタックExchangeおよび私が使用している他のサイトの両方で見つけたあるいくつかのGoogleのスクリプトコードです:

function runReplaceInSheet(){ 
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("StudentTimetableEntry"); 

    // Replace Subject Names 

     replaceInSheet(sheet,/\d\dART\d\d/g,"Art"); 
     replaceInSheet(sheet,/\d\dCCL\d\d/g,"Communication & Culture"); 
     replaceInSheet(sheet,/\d\dDLT\d\d/g,"Digital Technology"); 
     replaceInSheet(sheet,/\d\dDRA\d\d/g,"Drama"); 

    // Replace Staff Names 

     replaceInSheet(sheet,'TED','Tahlee Edward'); 
     replaceInSheet(sheet,'TLL','Tyrone LLoyd'); 
     replaceInSheet(sheet,'TMA','Timothy Mahone'); 
     replaceInSheet(sheet,'TQU','Tom Quebec'); 
     } 

     function replaceInSheet(sheet, to_replace, replace_with) { 
      //get the current data range values as an array 
      var values = sheet.getDataRange().getValues(); 

      //loop over the rows in the array 
      for(var row in values){ 

      //use Array.map to execute a replace call on each of the cells in the row. 
      var replaced_values = values[row].map(function(original_value){ 
       return original_value.toString().replace(to_replace,replace_with); 
      }); 

      //replace the original row values with the replaced values 
      values[row] = replaced_values; 
      } 

      //write the updated values to the sheet 
      sheet.getDataRange().setValues(values); 
     } 

これは完璧に動作します。しかし、私は150人以上のスタッフ名と、おおよそ同じ件数の件名を持っています。プロセスは最大時間に達し、これをコーディングするより良い方法があるはずです。

私は代替方法を考えていますが、それを使用するスタッフにとってはできるだけ簡単にする必要があることを覚えておいてください。

答えて

0

スクリプトでgetValuesとsetValuesを呼び出すたびに、かなりのオーバーヘッドが発生し、スクリプトが遅くなります。 (Google app script timeout ~ 5 minutes?)上記のスクリプトを変更して、getValuesを1回、setValuesを1回呼び出すようにしました。このコードは、変更されたタイムテーブルを貼り付ける前にすべての置換えを適用します。

function runReplaceInSheet(){ 
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("StudentTimetableEntry"); 
      // get the current data range values as an array 
      // Lesser calls to access the sheet, lower overhead 
      var values = sheet.getDataRange().getValues(); 

      // Replace Subject Names 

      replaceInSheet(values,/\d\dART\d\d/g,"Art"); 
      replaceInSheet(values,/\d\dCCL\d\d/g,"Communication & Culture"); 
      replaceInSheet(values,/\d\dDLT\d\d/g,"Digital Technology"); 
      replaceInSheet(values,/\d\dDRA\d\d/g,"Drama"); 

     // Replace Staff Names 

      replaceInSheet(values,'TED','Tahlee Edward'); 
      replaceInSheet(values,'TLL','Tyrone LLoyd'); 
      replaceInSheet(values,'TMA','Timothy Mahone'); 
      replaceInSheet(values,'TQU','Tom Quebec'); 
      //write the updated values to the sheet, again less call;less overhead 
      sheet.getDataRange().setValues(values); 
      } 

function replaceInSheet(values, to_replace, replace_with) { 


       //loop over the rows in the array 
       for(var row in values){ 

       //use Array.map to execute a replace call on each of the cells in the row. 
       var replaced_values = values[row].map(function(original_value){ 
        return original_value.toString().replace(to_replace,replace_with); 
       }); 

       //replace the original row values with the replaced values 
       values[row] = replaced_values; 
       } 



      } 

それでもタイムアウトに問題がある場合は、私の提案は、セットアップは、チェーン機能を助けるためにトリガされ、このコードを試してみてください。

+0

チャームのように働いた! – Stephen

+0

若い精神を育てるのを助けるうれしいこと:) –