简体   繁体   English

用于复制和重命名工作表和名称的Google脚本基于单元格引用

[英]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.

相关问题 用于匹配和复制基于另一个相邻单元格的 Google 表格脚本 - Google sheet script to match and copy based from another adjacent cell (Google API)单元格参考中的动态工作表和工作表创建脚本的名称 - (Google API) Dynamic Sheet and Name of Sheet Creation Script from Cell Reference 根据条件将单元格值从一个Google工作表复制到另一个 - Copy cell values from one google sheet to another based on condition 根据单元格值滚动一行到屏幕顶部:Google Sheet Script - Scroll a row to the top of the screen based on cell value: Google Sheet Script 清除单元格谷歌工作表脚本 - Clear cell google sheet script Google表格脚本读取单元格“ a”的值并根据单元格“ a”的值写入单元格“ b” - Google Sheet script that reads cell “a” and writes to cell “b” based off cell “a” value Google Apps脚本:将单元格值复制到另一个工作表中,条件是要复制并放置该值 - Google Apps Script: Copy cell value to another sheet with condition to copy and position the value Google Script可以从文件夹中的每个工作表复制到基于文件夹的主工作表吗? - Google Script to copy from each sheet in folder to a master sheet in folder based? Google 工作表应用程序脚本 - 根据循环条件将数据从一张工作表复制到另一张工作表 - Google sheet app script - Copy data from one sheet to another based on condition with loop 根据 Google Sheet 脚本第一列合并单元格中的日期从工作表锁定异常中删除行 - Remove rows from sheet lock exception based on date in merged cell in first column in Google Sheet script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM