關(guān)于Stack Exchange的第一個(gè)問題,所以希望它有意義.
一些背景:我在學(xué)校環(huán)境中工作,并協(xié)助學(xué)習(xí)支持人員為某些學(xué)生創(chuàng)建更可讀的時(shí)間表.
他們正在復(fù)制我們網(wǎng)站的時(shí)間表數(shù)據(jù),其中包含主題代碼,教師姓名和房間號碼.它與您在下圖中看到的格式完全相同 – 我只是將其復(fù)制到Google表格中.
我基本上需要對所有這些代碼執(zhí)行批量查找和替換,并完全展開它們以便例如主題代碼. 01ENG02成為“英語”和教師代碼,例如JBO成為“Joe Bloggs”
我有一個(gè)完整的列表,我需要擴(kuò)展到的代碼 – 它是如何最好地實(shí)現(xiàn)它.
以下是我在Stack Exchange和我正在使用的其他網(wǎng)站上找到的一些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個(gè)員工名稱,并且主題代碼的數(shù)量大致相同.這個(gè)過程達(dá)到了最長時(shí)間,我確信必須有一個(gè)更好的編碼方式.
我會(huì)考慮其他方法,但請記住,對于將要使用它的員工來說,它需要盡可能簡單明了.
解決方法:
每次在腳本中調(diào)用getValues和setValues時(shí),都會(huì)產(chǎn)生相當(dāng)大的開銷成本并降低腳本速度. (Google app script timeout ~ 5 minutes?)我修改了你的上面的腳本,以便為getValues和1調(diào)用setValues進(jìn)行1次調(diào)用.在將修改后的時(shí)間表粘貼回工作表之前,代碼將所有替換應(yīng)用于內(nèi)存中的數(shù)組.
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; }}
嘗試使用此代碼,如果仍有超時(shí)問題,我的建議是設(shè)置觸發(fā)器以幫助鏈功能.
來源:https://www.icode9.com/content-1-480651.html