2017-02-10 163 views
0

Stack Exchange的第一个问题,所以希望它是有道理的。谷歌应用程序脚本多个查找和替换谷歌表

一些背景:我在学校环境中工作,并协助学习支持人员为某些学生创建更具可读性的时间表。

他们正在从我们的网站复制包含学科代码,教师姓名和房间号码的时间表数据。它与您在下图中看到的格式完全相同 - 我只是将它复制到Google表格中。

GoogleSheetsImage

我基本上需要执行批量查找和替换所有这些代码,并充分扩大他们,使主语代码例如01ENG02成为“英语”,教师代码例如JBO成为“Joe Bloggs”

我有我需要的代码扩展到的完整列表 - 它只是如何最好地实现这一点。

下面是一些谷歌的脚本代码中,我已经在两个栈Exchange和其他网站发现了我使用的是:

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。该代码在粘贴修改后的时间表之前应用所有替换。

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

很高兴帮助教育年轻人的头脑:) –

相关问题