use*_*137 7 javascript google-sheets google-apps-script
我是谷歌脚本的新手,我需要将当前活动工作表复制到新工作表,然后根据单元格值重命名此工作表.我的问题是单元格值是一个日期和下面的代码工作,而是重新命名工作表2014年5月30日它返回数字等效41789.我如何粘贴实际日期.
function CreateNewTimesheet() {
// The code below makes a duplicate of the active sheet
var ss = SpreadsheetApp.getActiveSpreadsheet()
SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet();
// The code below will rename the active sheet to Month End date based on cell O3
var myValue = SpreadsheetApp.getActiveSheet( ).getRange("O3").getValue();
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet(myValue);
}
Run Code Online (Sandbox Code Playgroud)
您需要将值格式化为字符串,然后使用它来设置名称。
var localTimeZone = "Europe/London";
var dateFormatForFileNameString = "yyyy-MM-dd'at'HH:mm:ss";
function CreateNewTimesheet() {
// The code below makes a duplicate of the active sheet
var ss = SpreadsheetApp.getActiveSpreadsheet()
SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet();
// The code below will rename the active sheet to Month End date based on cell O3
var myValue = SpreadsheetApp.getActiveSheet( ).getRange("O3").getValue();
var dateString = getDateString_(myValue);
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet(dateString);
}
//Function to get Date as a string
function getDateString_(dateValue) {
return Utilities.formatDate(dateValue, localTimeZone,
dateFormatForFileNameString);
}
Run Code Online (Sandbox Code Playgroud)
希望有帮助。
| 归档时间: |
|
| 查看次数: |
5295 次 |
| 最近记录: |