【发布时间】:2019-10-04 16:59:26
【问题描述】:
我使用了在大型 Google 电子表格中发布的 here 脚本,将字符串替换为数字。
效果很好,但问题是,日期变成了字符串。
我认为问题出在方法toString(),但我找不到任何替代方法来使代码工作。
=> 有没有人想办法避免这个问题?
脚本
function runReplaceInSheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet3");
// get the current data range values as an array
// Fewer calls to access the sheet -> lower overhead
var values = sheet.getDataRange().getValues();
// Replace
replaceInSheet(values, 'datateam', '42007860');
// 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-apps-script google-sheets