格式为 nnnn-nnnn 的字符串在电子表格中显示为日期或其他错误
A string of form nnnn-nnnn is displayed in a spreadsheet as a date or otherwise incorrectly
我有一个在我的测试环境中一直使用的脚本,它通过从时间戳解析年份并填充响应索引来以编程方式创建跟踪号。
function setTrackingNumber(ss, lastRowInx, createDateColumn) //This block generates and stores a tracking number in Column AU on the backend
{
var padTrackNo = "" + lastRowInx;
var trackSize = 4;
var trackingNumberColumn = createDateColumn-3; //trackingNumberColumn is currently in AU (Column 47) Calculating using it's relative position to createDateColumn Position
if (ss.getRange(lastRowInx, trackingNumberColumn).getValue() == "") // so that subsequent edits to Google Form don't overwrite original tracking number
{
if (padTrackNo > trackSize)
{
var padTrackNo = pad(padTrackNo, trackSize);
}
else {} //do nothing
var shortYear = setShortYear(ss, lastRowInx, createDateColumn);
var trackingNumber = shortYear + "-" + padTrackNo;
var createTrackingNumber = ss.getRange(lastRowInx, trackingNumberColumn);
createTrackingNumber.setValue(trackingNumber);
}
else {} //should do nothing
return;
}//This is the end of the setTrackingNumber function
function setShortYear(ss, lastRowInx, createDateColumn)
{
var newCreateDate = ss.getRange(lastRowInx,createDateColumn).getValue();
var strDate = "" + newCreateDate;
var splitDate = strDate.split(" ");
var trimYear = splitDate[3];
var shortYear = trimYear;
return shortYear;
}//This is the end of the shortYear function
function pad(padTrackNo, trackSize)
{
while (padTrackNo.length < trackSize)
{
padTrackNo = "0"+padTrackNo;
}
return padTrackNo;
}//This is the end of pad function
这让我很感动 test result which is as expected ex. 2016-0005. However when we added it to another production sheet it seemed to work with test data and then production data showed up like a date 3/1/2016. production result - first cell。
我认为它一定只是将字符串格式化为日期,因为数字所以我尝试将列格式化为纯文本,但只是将日期更改为日期的纯文本版本。
我认为这可能类似于我在这个问题 中按照@SandyGood 的建议需要指定格式,所以我尝试通过更改 [ 将数字格式设置为 [0000-0000] =15=]
createTrackingNumber.setValue(trackingNumber);
到
createTrackingNumber.setValue(trackingNumber).setNumberFormat("0000-0000");
导致 [生产结果 - 第二个单元格] 再次与预期结果不匹配。
奇怪的是,一些提交似乎工作得很好,就像[生产结果-第三个细胞]。在过去的 3 天和大约 10 个记录中,一切都很好,然后变坏了,然后变好了,变坏了,然后又变好了。我不太确定还有什么可以尝试调试这种奇怪的行为。
注意:我不得不将日期解析为字符串,因为我无法从初始时间戳获取的创建日期正确解析日期。
据我了解,“2016-0005”不是数字而是字符串,因此包含它的单元格应采用纯文本格式。使用脚本,这可以通过
range.setNumberFormat('@STRING@')
(source),这必须在 之前 将值设置为单元格。像这样:
createTrackingNumber.setNumberFormat('@STRING@').setValue(trackingNumber);
我有一个在我的测试环境中一直使用的脚本,它通过从时间戳解析年份并填充响应索引来以编程方式创建跟踪号。
function setTrackingNumber(ss, lastRowInx, createDateColumn) //This block generates and stores a tracking number in Column AU on the backend
{
var padTrackNo = "" + lastRowInx;
var trackSize = 4;
var trackingNumberColumn = createDateColumn-3; //trackingNumberColumn is currently in AU (Column 47) Calculating using it's relative position to createDateColumn Position
if (ss.getRange(lastRowInx, trackingNumberColumn).getValue() == "") // so that subsequent edits to Google Form don't overwrite original tracking number
{
if (padTrackNo > trackSize)
{
var padTrackNo = pad(padTrackNo, trackSize);
}
else {} //do nothing
var shortYear = setShortYear(ss, lastRowInx, createDateColumn);
var trackingNumber = shortYear + "-" + padTrackNo;
var createTrackingNumber = ss.getRange(lastRowInx, trackingNumberColumn);
createTrackingNumber.setValue(trackingNumber);
}
else {} //should do nothing
return;
}//This is the end of the setTrackingNumber function
function setShortYear(ss, lastRowInx, createDateColumn)
{
var newCreateDate = ss.getRange(lastRowInx,createDateColumn).getValue();
var strDate = "" + newCreateDate;
var splitDate = strDate.split(" ");
var trimYear = splitDate[3];
var shortYear = trimYear;
return shortYear;
}//This is the end of the shortYear function
function pad(padTrackNo, trackSize)
{
while (padTrackNo.length < trackSize)
{
padTrackNo = "0"+padTrackNo;
}
return padTrackNo;
}//This is the end of pad function
这让我很感动 test result which is as expected ex. 2016-0005. However when we added it to another production sheet it seemed to work with test data and then production data showed up like a date 3/1/2016. production result - first cell。
我认为它一定只是将字符串格式化为日期,因为数字所以我尝试将列格式化为纯文本,但只是将日期更改为日期的纯文本版本。
我认为这可能类似于我在这个问题 到 导致 [生产结果 - 第二个单元格] 再次与预期结果不匹配。 奇怪的是,一些提交似乎工作得很好,就像[生产结果-第三个细胞]。在过去的 3 天和大约 10 个记录中,一切都很好,然后变坏了,然后变好了,变坏了,然后又变好了。我不太确定还有什么可以尝试调试这种奇怪的行为。 注意:我不得不将日期解析为字符串,因为我无法从初始时间戳获取的创建日期正确解析日期。 createTrackingNumber.setValue(trackingNumber);
createTrackingNumber.setValue(trackingNumber).setNumberFormat("0000-0000");
据我了解,“2016-0005”不是数字而是字符串,因此包含它的单元格应采用纯文本格式。使用脚本,这可以通过
range.setNumberFormat('@STRING@')
(source),这必须在 之前 将值设置为单元格。像这样:
createTrackingNumber.setNumberFormat('@STRING@').setValue(trackingNumber);