ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript – Google Apps中用于多个查找和替换的Google Apps脚本

2019-10-02 10:36:05  阅读:296  来源: 互联网

标签:javascript google-sheets google-sheets-api


关于Stack Exchange的第一个问题,所以希望它有意义.

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

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

GoogleSheetsImage

我基本上需要对所有这些代码执行批量查找和替换,并完全展开它们以便例如主题代码. 01ENG02成为“英语”和教师代码,例如JBO成为“Joe Bloggs”

我有一个完整的列表,我需要扩展到的代码 – 它是如何最好地实现它.

以下是我在Stack Exchange和我正在使用的其他网站上找到的一些Google Scripts代码:

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个员工名称,并且主题代码的数量大致相同.这个过程达到了最长时间,我确信必须有一个更好的编码方式.

我会考虑其他方法,但请记住,对于将要使用它的员工来说,它需要尽可能简单明了.

解决方法:

每次在脚本中调用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
  //  Fewer 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 all updated values to the sheet, at once
  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;
  }
}

尝试使用此代码,如果仍有超时问题,我的建议是设置触发器以帮助链功能.

标签:javascript,google-sheets,google-sheets-api
来源: https://codeday.me/bug/20191002/1842544.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有