简体   繁体   English

在 Google 表格中使用 Google Apps 脚本时如何防止日期变成字符串?

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

在此处输入图像描述

  • You want to replace values of Spreadsheet.您想要替换电子表格的值。
    • In your script, you want to replace from datateam to 42007860 .在您的脚本中,您希望将42007860 datateam
  • You want to achieve this using Google Apps Script.您想使用 Google Apps 脚本实现此目的。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several answers.请认为这只是几个答案之一。

Pattern 1:模式一:

In this pattern, getDisplayValues() is used instead of getValues() .在此模式中,使用getDisplayValues()代替getValues()

Modified script修改后的脚本

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);
}

Pattern 2:模式二:

In this pattern, TextFinder is used instead of replaceInSheet() and setValues() .在此模式中,使用 TextFinder 代替replaceInSheet()setValues()

Modified script修改后的脚本

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
}

References:参考:

If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

I would check the template, your actual XLS file.我会检查模板,你的实际 XLS 文件。 Set the type of the cells before entering the data.在输入数据之前设置单元格的类型。 I assume this might be your problem not your actual code.我认为这可能是您的问题,而不是您的实际代码。 This is in the Home tab.这是在主页选项卡中。

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能让一个单元格在编辑时发布它旁边的编辑日期? 在 Google 表格中使用 Google Apps 脚本 - How can I have a cell that when edited publishes it's edit date next to it? In Google Sheets using a Google Apps Script 在使用谷歌应用脚本将数据从谷歌表格注入谷歌幻灯片期间将日期转换为 dd/mm/yyyy - Convert date to dd/mm/yyyy during injection of data from google sheets to google slides using google apps script 问题:我想学习如何使用谷歌应用脚本将数字从网站导入谷歌表格 - Problem: I want to learn how to import a number from a website to google sheets using google apps script 使用 Apps 脚本在 Google 表格中自动编号 - Autonumbering in Google Sheets Using Apps Script 如何从表格中计算谷歌应用脚​​本中的持续时间? - How do I calculate duration in google apps script from sheets? Google Apps Script - 如何将此代码应用于 Google 表格中的多个表格 - Google Apps Script - how to applies this code for multiple sheets in google sheets 从 Apps 脚本显示消息 Google 表格 - Display message Google Sheets from Apps Script Google 应用程序脚本一直将我的计算视为字符串,如何解决? - Google apps script keep considering my calculation as string, how to fix it? 如何将 Google Apps 脚本应用于多个工作表 - How to apply Google Apps Script to multiple sheets 如何通过使用 arrays 从 Google 表格中提取数据并格式化来优化 Apps 脚本代码? - How to optimise Apps Script code by using arrays to pull data from Google Sheets and format it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM