Google apps script 谷歌脚本过滤掉importrange上整个工作表中不需要的字符
所以我想运行一个正则表达式来去除所有的非ascii。 正则表达式为:[^\x00-\x7F] 是否有办法在导入时执行此操作,或者我需要导入所有内容并逐个单元格查找字符并替换为 我用来导入所有内容的函数是:Google apps script 谷歌脚本过滤掉importrange上整个工作表中不需要的字符,google-apps-script,google-sheets,Google Apps Script,Google Sheets,所以我想运行一个正则表达式来去除所有的非ascii。 正则表达式为:[^\x00-\x7F] 是否有办法在导入时执行此操作,或者我需要导入所有内容并逐个单元格查找字符并替换为 我用来导入所有内容的函数是: function createNewSheet(newSheetName) { var ss = SpreadsheetApp.getActiveSpreadsheet(); yourNewSheet = ss.insertSheet(); yourNewSheet.setName
function createNewSheet(newSheetName) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
yourNewSheet = ss.insertSheet();
yourNewSheet.setName(newSheetName);
yourNewSheet.getRange('A1').setFormula('=query(IMPORTRANGE(\"https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxx/edit#gid=0\", \"Master!A:K\"), \"SELECT Col2, Col3, Col4, Col5, Col6, Col7, Col8, Col9, Col10, Col11 where Col1 = \'"& \'Select Your Event\'!A3 &"\' Order By Col9, Col10",1)')
pasteValues(newSheetName);
SpreadsheetApp.flush();
yourNewSheet.autoResizeColumns(7,4);
}
我从这个答案中找到了解决方案: 我能够以这种方式实现所需的正则表达式,并扫描和替换工作表中的所有非ASCII字符
function runReplaceInSheet(newSheetName){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(newSheetName);
// get the current data range values as an array
// Fewer calls to access the sheet -> lower overhead
var values = sheet.getDataRange().getValues();
// Search for all non-ASCII values and just replace them with nothing
regex = new RegExp ('[^\x00-\x7F]', 'gi');
replaceInSheet(values, regex, "");
// 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;
}
}
使用内置的查找和替换功能录制宏。我可以从google脚本调用宏吗?可以。宏是谷歌脚本。如果你遇到麻烦,就把宏代码复制成脚本。啊,我现在明白了。谢谢。由于某些原因,我无法让宏记录任何查找/替换活动。但我明白了。谢谢你的提示。