2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

エクセルの特定背景色の行のみSlackへ通知する

Posted at

概要

エクセルで管理しちゃっている情報をなんとかエクセルを開かず内容チェックしたい。

今調べたいエクセルシートは状態をセルの背景色で管理しているので、未処理のものだけでも毎日チェックして報告してもらいたい。

コード

実際にはネットワーク上のディレクトリにあるので、
マシンにはマウントしてそのパスで参照、
出力にはネットワークへのパスに変換する処理を追加しています。


var target = 'C:/Users/{user}/Desktop/tmp/hoge.xlsx'

var XLSX = require('xlsx');
var workbook = XLSX.readFile(target, {cellStyles:true});
var worksheet = workbook.Sheets[workbook.SheetNames[0]];

var Slack = require('slack-node')
var slack = new Slack()
slack.setWebhook('自分のSlackの投稿先')

var sendSlack = (text, attachments) => {
	slack.webhook({
		text:text,
		channel:'#random',
		username: '検討状況',
		icon_emoji: ':stopwatch:',
		attachments: attachments,
	}, (err, response)=>{
	})
}

var makeAttachment = (title, color, fields) => {

	return {
		title:title,
		title_link:target,
		color:color,
		fields,fields,
	}
}

var makeField = (title, value)=> {
	return {title:title, value:value, short:true}
}

var fetchInfo = (sheet, number) => {
	return {
		title: worksheet['B' + i].v,
		priority:worksheet['D' + i].v,
		make:worksheet['E' + i].w,
		author:worksheet['F' + i].v,
		limit:worksheet['G' + i].w,
	}
}


var statMessage = {'FF0000':'未検討','FFFF00':'検討中'}

var attachments = [];

for(var i = 9; cell = worksheet['A' + i]; i++) {

	var fields = [];

	switch (cell.s.fgColor.rgb) {
		case 'FF0000': // 未検討
		//case 'FFFF00': // 検討中 長くなりすぎるのでコメントアウト
			var info = fetchInfo(worksheet, i)
			fields.push(makeField('状況', statMessage[cell.s.fgColor.rgb]))
			fields.push(makeField('優先度', info.priority))
			fields.push(makeField('記入者', info.author))
			fields.push(makeField('記入日', info.make))
			fields.push(makeField('解決期限', info.limit))
			attachments.push(makeAttachment(info.title, cell.s.fgColor.rgb, fields))
			break;
	}
	attachments.push(makeAttachment(cell))
}

sendSlack('検討状況\n' + target, attachments)


今回の対象はAnの背景色で状態、同行に固定列で情報が入っているので、対象セルの横列を参照していきます。

オプションで{cellStyles:true}にしてやることと、
今回のエクセルではbgColorではなくfgColorにセルの色が入っていることに注意。

Slack通知サンプル

エクセルパースJPG.JPG

使用ライブラリ

その他参考

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?