Skip to content

Instantly share code, notes, and snippets.

@naosim
naosim / comDate.js
Created November 23, 2019 07:03
予算管理で必要な日付への変換をする。スプレッドシートから呼び出すカスタム関数用
function comDate(dates, typesCsv) {
var convert = function(d, type) {
if(type == '年月') {
return d.getYear() + ('0' + (d.getMonth() + 1)).slice(-2);
}
var nendo = 'FY' + (d.getMonth() + 1 < 4 ? d.getYear() - 1: d.getYear());
if(type == '年度') {
return nendo;
}
if(type == '半期') {
@naosim
naosim / SeachMail.js
Created September 15, 2019 21:58
gmailを検索する
/**
* メールを検索する
* 注意: 内部でGmailApp.searchを利用しているため、検索結果は多めに取れる。厳密にするには戻り値に対してもう一度フィルターをかける必要がある。
*
* @param {string} searchText
* @param {object} option 省略可
* @return {GmailMessage[]} 日付の昇順でソート済み
*/
function searchMail(searchText, option) {
// optionの初期値セットアップ
@naosim
naosim / SheetDb.js
Last active July 17, 2019 01:26
【GAS】シートをDBのように扱う。オブジェクト版
function SheetDb(spreadSheet) {
/**
* レコードの全取得
*/
function findAll(name /* シート名 */) {
var sheet = spreadSheet.getSheetByName(name);
var table = sheet.getDataRange().getValues()
return JSON.parse(JSON.stringify(table));
}
@naosim
naosim / GithubApi.js
Created July 16, 2019 22:46
【GAS】GithubApiライブラリ
function GithubApi(
accessToken,
owner,
repo
) {
var requestOptions = {
headers: { Authorization: 'token ' + accessToken }
}
function exec(url) {
@naosim
naosim / loadLibForMethods.js
Created July 16, 2019 22:42
【GAS】複数メソッドをロードする
/**
* 複数メソッドをロードする
* 依存:
* - retryFetch https://gist.github.com/naosim/8fc6033b6f92e426eddc0424e7f7aa71
*/
function loadLibForMethods(methodNames, url) {
eval(retryFetch(url))
var result = {};
methodNames.forEach(function(name) {
@naosim
naosim / retryFetch.js
Created July 16, 2019 22:12
【GAS】リトライありのfetch
function retryFetch(url) {
var lastError = null;
for(var i = 0; i < 3; i++) {
try {
var res = UrlFetchApp.fetch(url)
if(res.getResponseCode() == 200) {
return res.getContentText("UTF-8")
} else {
lastError = 'HTTPステータスコードが200以外: ' + res.getResponseCode() + ', ' + url;
}
@naosim
naosim / loadLibForOneMethod.js
Last active May 11, 2021 19:58
【GAS】githubやgist上のライブラリを読み込むスニペット
/**
* 外部ファイルからjsを読み込む
* githubやgists上にあるコードを使いたい時用
*/
function loadLibForOneMethod(mainMethodName, url) {
// リトライありのfetch
function retryFetch(url) {
var lastError = null;
for(var i = 0; i < 3; i++) {
try {
@naosim
naosim / sheet.js
Last active July 12, 2019 22:23
GASでスプレッドシートを操作する
/**
* レコードの全取得
*/
function findAll(name /* シート名 */) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name);
var table = sheet.getDataRange().getValues()
return JSON.parse(JSON.stringify(table));
}
/**
@naosim
naosim / lastupdate.js
Created December 18, 2018 02:02
ディレクトリ配下で最後に更新したファイルを取得する
// 最後に更新されたファイルを取得する
// 利用例
// ls -l | node lastupdate.js
// => 2018-12-18 10:50 ./worklog/index.js
//
// サブディレクトリも対象にするならこう
// find . -type f | xargs ls -l | node worklog index.js
//
// 事前準備
// npmで'textpipe'を入れておくこと
@naosim
naosim / table.js
Created June 12, 2018 21:59
markdownからcreatetableを作るメモ
// node . inputfile outRootDir
if(process.argv.length == 2) {
const msg = 'USAGE\nnode . inputMarkdownFile outRootDir'
console.log(msg)
throw msg
}
const inputMarkdownFile = process.argv[2]
const outRootDir = process.argv[3]