如何拆分字符串并获取日历事件 ID?

How to split a string and get the calendar event ID?

在我的传播sheet 中,我有一个超链接日历事件的公式。在 sheet:

中看起来像这样
=HYPERLINK("https://www.google.com/calendar/u/0/r/eventedit/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","View Invite")

在我的脚本中,我希望它分析该单元格并将其拆分以通过 base64 解码专门获取 XXXXX 部分。然后,在该解码中是事件 ID 和日历 ID,我特别想要事件 ID。

var viewInvite = sheet.getRange(index, 13).getValue();

var beginningPiece = '=HYPERLINK("https://www.google.com/calendar/u/0/r/eventedit/';
var endPiece = '","View Invite")';
var splitEventId = Utilities.newBlob(Utilities.base64Decode(beginningPiece.split("=")[1])).getDataAsString().split(" ")[0];
Logger.log(splitEventId);

如何从公式中获取事件 ID?我尝试过像上面那样的场景,但我无法弄清楚

解释:

  • 使用getFormula从sheet得到公式,否则如果你使用getValue你会得到View Invite因为那是值公式 returns.

  • 您不需要使用正则表达式,只需 this simple/classical 方法。

解决方案:

const viewInvite = sheet.getRange(index, 13).getFormula();
const splitEventId  = viewInvite.substring(
                        viewInvite.lastIndexOf("r/") + 2, 
                        viewInvite.lastIndexOf("/"));
console.log(splitEventId);

最小可重现示例:

const viewInvite = '=HYPERLINK("https://www.google.com/calendar/u/0/r/eventedit/';
const splitEventId  = viewInvite.substring(
                        viewInvite.lastIndexOf("r/") + 2, 
                        viewInvite.lastIndexOf("/"));
console.log(splitEventId);