【问题标题】:How to create a note that include the day automatically?如何自动创建包含日期的便笺?
【发布时间】:2020-10-27 16:39:28
【问题描述】:

我想看看如何在便笺中自动获取日期,以便跟进,

// Add the date when you create a note

function Notes() 
{
   
  var app= SpreadsheetApp;
  var activeSheet = app.getActiveSpreadsheet().getActiveSheet(); // Returns the active shee
  var activeRange= activeSheet.getActiveRange() // Returns the active Range
  
  activeRange.setNote(Date());  // Set the date in the note


  
}

此代码有效,但我得到此代码:Thu Oct 22 2020 08:28:09 GMT+0200(中欧夏令时间) 例如,我只想获得 22/10/2020 08:28:09。

【问题讨论】:

    标签: javascript google-apps-script google-sheets


    【解决方案1】:

    您只需要将日期转换为所需的格式dd/MM/yyyy HH:mm:ss。您可以使用Utilities.formatDate() 来实现:

    function Notes() 
    {
      var app= SpreadsheetApp;
      var activeSheet = app.getActiveSpreadsheet().getActiveSheet(); // Returns the active shee
      var activeRange= activeSheet.getActiveRange() // Returns the active Range
      var today = Utilities.formatDate(new Date(), app.getActive().getSpreadsheetTimeZone(), "dd/MM/yyyy HH:mm:ss");
      
      activeRange.setNote(today);  // Set the date in the note
    }
    

    【讨论】:

      【解决方案2】:

      在JS中打印Date的方法有很多

      var d = new Date()
      
      console.log(d.toString())
      console.log(d.toISOString())
      console.log(d.toDateString())
      console.log(d.toGMTString())
      console.log(d.toUTCString())
      console.log(d.toTimeString())
      console.log(d.toLocaleString())
      console.log(d.toLocaleDateString())
      console.log(d.toLocaleTimeString())

      您还可以使用此 getter 方法构建自己的格式:

      var d = new Date()
      
      console.log(d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate())

      请记住,日期是本地对象,取决于浏览器的区域设置和时区。

      【讨论】:

      • 但我想在注释中看到这一点,而不是在控制台日志中。感谢您的帮助!
      • 只需将 Date() 替换为 activeRange.setNote(Date()) 中所需的格式
      • 我这样做了,但它不起作用。 activeRange.setNote(d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate()+ "TM") 提前致谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多