自动颜色 Google 基于倒数第二个字符的日历事件标题

Auto Color Google Calendar event title based on second last character

我正在尝试根据标题中的倒数第二个字符自动为事件着色,当前函数可以使用最后一个字符来执行此操作,因为已使用 endsWith,但是有没有办法更改它读取倒数第二个字符而不是最后一个字符。

function ColorEvents() {

  var calendar = CalendarApp.getCalendarById("ID@group.calendar.google.com")
  var beginDate = new Date(2019, 0, 1); 
  var endDate = new Date(2080, 10, 1);
  Logger.log("found number of calendars: " + calendar.length);

    var events = calendar.getEvents(beginDate, endDate);
    for (var j=0; j<events.length; j++) {
      var e = events[j];
      var title = e.getTitle();
      if (title.toLowerCase().endsWith("a")) {
        e.setColor(CalendarApp.EventColor.RED);
      }

我相信你的目标如下。

  • 您想使用 Google Apps 脚本检查字符串值的倒数第二个字符。

为了检查倒数第二个字符,当你想使用endsWith时,下面的修改如何?

发件人:

if (title.toLowerCase().endsWith("a")) {

收件人:

if (title.toLowerCase().endsWith("", title.length - 1)) {
  • 在此示例脚本中,</code> 用作脚本中的搜索字符。</li> </ul> <h3>测试:</h3> <p><div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="false"> <div class="snippet-code snippet-currently-hidden"> <pre><code>const search = ""; const str = ["abde", "abce", "abcd"]; const res = str.map(s => s.endsWith(search, s.length - 1)); console.log(res) // <--- [ false, true, false ]

参考:

已添加:

与其他方法一样,将包含unicode字符串的字符串值与spread syntax拆分并进行比较。

修改后的脚本:

发件人:

if (title.toLowerCase().endsWith("a")) {

收件人:

var search = "";
var ar = [...title.toLowerCase()];  // In your case, I'm not sure whether "toLowerCase()" is required.
if (ar[ar.length - 2] == search) {