const fs = require('fs');
const path = require('path');
if (process.argv.length < 3) {
console.error('使い方: node index.js <フォルダパス>');
process.exit(1);
}
//const targetDir = process.argv[2];
if (path.basename(targetDir) !== 'xl') {
console.error('指定したフォルダ名は "xl" である必要があります。');
process.exit(1);
}
const worksheetsDir = path.join(targetDir, 'worksheets');
if (!fs.existsSync(worksheetsDir) || !fs.statSync(worksheetsDir).isDirectory()) {
console.error('worksheets フォルダが存在しません。');
process.exit(1);
}
const targetDir = worksheetsDir;
fs.readdir(targetDir, (err, files) => {
if (err) {
console.error('ディレクトリの読み込みに失敗:', err);
process.exit(1);
}
files.filter(file => file.endsWith('.xml')).forEach(file => {
const filePath = path.join(targetDir, file);
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(`${file} の読み込みに失敗:`, err);
return;
}
// sheetProtectionタグを削除
const newData = data.replace(//g, '');
fs.writeFile(filePath, newData, 'utf8', err => {
if (err) {
console.error(`${file} の書き込みに失敗:`, err);
} else {
console.log(`${file} の sheetProtection タグを削除しました。`);
}
});
});
});
});
const workbookXmlPath = path.join(path.dirname(targetDir), 'workbook.xml');
if (fs.existsSync(workbookXmlPath)) {
fs.readFile(workbookXmlPath, 'utf8', (err, data) => {
if (err) {
console.error('workbook.xml の読み込みに失敗:', err);
return;
}
// 削除したいタグ名を指定(例: )
const tagPattern = //g;
const newData = data.replace(tagPattern, '');
if (newData !== data) {
fs.writeFile(workbookXmlPath, newData, 'utf8', err => {
if (err) {
console.error('workbook.xml の書き込みに失敗:', err);
} else {
console.log('workbook.xml の指定タグを削除しました。');
}
});
} else {
console.log('workbook.xml に指定タグは見つかりませんでした。');
}
});
} else {
console.log('workbook.xml が見つかりません。');
}