[英]How to keep the date from turning into a String when using a Google Apps Script in Google Sheets?
I have used a Script posted here in a large Google Spreadsheet, to replace a String with a Number.我使用了在大型 Google 电子表格中发布的脚本,将字符串替换为数字。
It works great, but the problem is, the date gets turned into a String .它很好用,但问题是,日期变成了 String 。
I believe that the problem is the method toString()
, but I couldn't find any alternative to make the code work.我认为问题出在toString()
方法上,但我找不到任何替代方法来使代码正常工作。
=> Does anyone have an idea to avoid this problem? => 有没有人有办法避免这个问题?
Script脚本
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; } }
Before前
After后
datateam
to 42007860
.在您的脚本中,您希望将42007860
datateam
If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several answers.请认为这只是几个答案之一。
In this pattern, getDisplayValues()
is used instead of getValues()
.在此模式中,使用getDisplayValues()
代替getValues()
。
Please modify your script as follows.请按如下方式修改您的脚本。 In this modification, the function of runReplaceInSheet()
is modified.本次修改,修改了runReplaceInSheet()
的function。
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().getDisplayValues(); // Modified
// Replace
replaceInSheet(values, 'datateam', '42007860');
// Write all updated values to the sheet, at once
sheet.getDataRange().setValues(values);
}
In this pattern, TextFinder is used instead of replaceInSheet()
and setValues()
.在此模式中,使用 TextFinder 代替replaceInSheet()
和setValues()
。
Please modify your script as follows.请按如下方式修改您的脚本。 In this modification, the function of runReplaceInSheet()
is modified.本次修改,修改了runReplaceInSheet()
的function。 And also, in this modification, replaceInSheet
is not used.而且,在此修改中,没有使用replaceInSheet
。
function runReplaceInSheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet3");
sheet.createTextFinder('datateam').replaceAllWith('42007860'); // Added
}
If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.