Skip to content

Instantly share code, notes, and snippets.

@hwada hwada/Code.gs

Created Jul 21, 2020
Embed
What would you like to do?
function parseTime(text) {
{
const match = /(\d\d?)(\d\d?)分/.exec(text);
if (match && match.length === 3) {
return [match[1], match[2]];
}
}
{
const match = /(\d\d?)時/.exec(text);
if (match && match.length === 2) {
return [match[1], 0];
}
}
{
const match = /(\d\d?):(\d\d?)/.exec(text);
console.log(match);
if (match && match.length >= 3) {
return [match[1], match[2]];
}
}
return [0, 0];
}
function parseDate(text) {
const [h, m] = parseTime(text);
{
const match = /(\d*?)(\d*?)(\d*?)日/.exec(text);
if (match && match.length >= 4) {
return new Date(match[1], match[2] - 1, match[3], h, m);
}
}
{
const match = /(\d*?)(\d*?)日/.exec(text);
if (match && match.length >= 3) {
const year = (new Date()).getFullYear(); // 年がなければ今年にする
return new Date(year, match[1] - 1, match[2], h, m);
}
}
{
// yyyy/mm/dd
const match = /(\d\d\d\d)\/(\d\d?)\/(\d\d?)/.exec(text);
if (match && match.length >= 4) {
return new Date(match[1], match[2] - 1, match[3], h, m);
}
}
{
// mm/dd
const match = /(\d\d?)\/(\d\d?)/.exec(text);
if (match && match.length >= 3) {
const year = (new Date()).getFullYear(); // 年がなければ今年にする
return new Date(year, match[1] - 1, match[2], h, m);
}
}
{
if (text.indexOf('本日') >= 0) {
const dt = new Date();
return new Date(dt.getFullYear(), dt.getMonth(), dt.getDate(), h, m);
}
}
return null;
}
function trigger(tweet) {
const calendar = CalendarApp.getDefaultCalendar();
const text = tweet.text;
const lines = text.split('\n');
const title = lines[0];
const startTime = parseDate(text);
if (!startTime) {
return;
}
const option = {
description: text
}
calendar.createEvent(title, startTime, startTime, option);
}
function doPost(e) {
var params = JSON.parse(e.postData.getDataAsString());
trigger(params);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.