Skip to content

Instantly share code, notes, and snippets.

@naosim
naosim / note.txt
Created March 22, 2024 13:32
てすと
テストだよ
@naosim
naosim / tableSchema.json
Created February 11, 2024 11:38
テーブル定義のためのJSON Schema
{
"type": "array",
"items": {
"$ref": "#/$defs/table"
},
"$defs": {
"table": {
"name": "table",
"type": "object",
"properties": {
@naosim
naosim / pocco.mjs
Last active December 22, 2023 20:29
pocco
import http from "http";
import fs from "fs";
import path from "path";
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const server = http.createServer((req, res) => {
const reqPath = req.url;
const sanitizedPath = path.normalize(reqPath).replace(/^(\.\.[\/\\])+/, '');
@naosim
naosim / ipv6.js
Last active November 28, 2023 13:37
ipv6
function getBitsAfter33(ipv6) {
function hexTo2Bits(v, bitLength = 4) {
if(v >= 10) {
v = "ABCDEF"[v - 10]
}
return (new Array(bitLength).fill("0").join("") + parseInt(v, 16).toString(2)).slice(-bitLength);
}
function bit2toHex(num) {
var p = num.split("").reverse().map((v, i) => Math.pow(2, i) * parseInt(v)).reduce((memo, v) => memo + v, 0);
if(p < 10) {
@naosim
naosim / todo.js
Last active July 31, 2023 14:11
spreadsheetでtaskAPIを使ってタスクを管理する
// いい感じに同期します
// 新規タスクの追加
// 変更点の更新
// GoogleTasksの読み込み
function update() {
new MainService().update();
}
function onOpen() {
const customMenu = SpreadsheetApp.getUi()
@naosim
naosim / todaysSchedule.js
Created April 17, 2023 11:26
Googleカレンダーの予定取得
function exportCalendarEvents() {
// カレンダーIDを入力してください
var calendarId = "primary";
// フォーマット
var format = "yyyy/MM/dd HH:mm:ss";
// 今日の日付を取得
var today = new Date();
// 今日の0時0分0秒を計算
var startOfDay = new Date(today.getFullYear(), today.getMonth(), today.getDate());
@naosim
naosim / gasGannt.js
Created March 23, 2023 02:22
GoogleSpreadSheetでガントチャートを使うときのユーティリティ
const headers = ["ID", "タスク名", "開始日", "終了日", "日数", "依存タスク"];
function setup() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("シート1");
if(sheet.getRange(1, 1).getValue() == "入力用") {
throw new Error("すでに実行済みです");
}
sheet.appendRow(["入力用"])
sheet.appendRow(headers)
@naosim
naosim / parseDateString.js
Created March 23, 2023 01:34
日付を返す関数。AIが書いたので意図通り動くかわかりません。
function parseDateString(dateStr, holidays) {
let date;
if (/^\d{4}\/\d{1,2}\/\d{1,2}$/.test(dateStr)) {
// 日付指定の場合
date = new Date(dateStr);
} else if (/^FY\d{2}\/\dQ$/.test(dateStr)) {
// 年度・四半期指定の場合
const fy = parseInt(dateStr.slice(2, 4), 10);
const q = parseInt(dateStr.slice(-2, -1), 10);
@naosim
naosim / addBusinessDays.js
Created March 23, 2023 00:15
営業日ベースでn日後の日付を計算する
function addBusinessDays(date, daysToAdd, holidays) {
// weekends: 0 = Sunday, 6 = Saturday
const weekendDays = [0, 6];
// create a set of holidays for O(1) lookup time
const holidaySet = new Set(holidays.map((holiday) => holiday.getTime()));
let businessDaysAdded = 0;
let currentDate = new Date(date.getTime());
while (businessDaysAdded < daysToAdd) {
// add one day to the current date
@naosim
naosim / getFilePathListById.js
Created March 17, 2023 13:10
GASでファイルIDをパスに変換する
function getFilePathListById(fileId) {
var file = DriveApp.getFileById(fileId);
return getParentPathList(file).map(v => 'G:\\' + v + '\\' + file.getName());
}
function iteratorToArray(iterator) {
var result = [];
while (iterator.hasNext()) {
result.push(iterator.next());
}