[英]Google script to copy and rename a sheet and name is based on a cell reference
I'm new to google scripts and I need to copy the current active sheet to a new sheet and then rename this sheet based on a cell value. 我是谷歌脚本的新手,我需要将当前活动工作表复制到新工作表,然后根据单元格值重命名此工作表。 My issue is the cell value is a date and the below code works but instead on renaming the sheet 30-May-2014 it is returning the numeric equivalent 41789. How can I paste the actual date. 我的问题是单元格值是一个日期和下面的代码工作,而是重新命名工作表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);
}
You would need to format the value into a string and then use it to set the name. 您需要将值格式化为字符串,然后使用它来设置名称。
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);
}
Hope that helps. 希望有所帮助。
您需要构造一个新的Date(myValue),然后在Utilities类中查找日期格式化函数。
Just using .getDisplayValue() in place of .getValue() in your code should do the trick. 在代码中使用.getDisplayValue()代替.getValue()应该可以解决问题。
It gets the value displayed in the cell as a string, taking into account things like date formatting, so when you then use that to rename your sheet the sheet name will be the same as what you see in your cell. 它将单元格中显示的值作为字符串显示,并考虑日期格式等内容,因此当您使用该值重命名工作表时,工作表名称将与您在单元格中看到的相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.