簡體   English   中英

用於在 Google 表格中進行多次查找和替換的 Google Apps 腳本

[英]Google Apps Script for Multiple Find and Replace in Google Sheets

關於 Stack Exchange 的第一個問題,希望它有意義。

一些背景:我在學校環境中工作,正在協助學習支持人員為某些學生創建更易讀的時間表。

他們從我們的網站上復制時間表數據,其中包含科目代碼、教師姓名和房間號。 它與您在下圖中看到的格式完全相同 - 我只是將其復制到 Google 表格中。

GoogleSheets 圖片

我基本上需要對所有這些代碼執行批量查找和替換,並完全擴展它們,以便主題代碼(例如 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 多個員工姓名,以及大致相同數量的主題代碼。 這個過程達到了最長時間,我相信一定有更好的編碼方式。

我會考慮其他方法,請記住,對於將要使用它的員工來說,它需要盡可能簡單明了。

每次在腳本中調用getValuessetValues時,都會涉及相當大的開銷成本,並且會減慢腳本速度。 Google 應用程序腳本超時 ~ 5 分鍾? )我修改了您上面的腳本,使 1 次調用getValues和 1 次調用setValues 在將修改后的時間表粘貼回工作表之前,代碼將所有替換應用於內存中的數組。

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;
  }
}

試試這段代碼,如果您仍然遇到超時問題,我的建議是設置觸發器以幫助鏈接函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2025 STACKOOM.COM