ken_nogi/NodeJS/SheetsProtectUnlock/index.js
Kenichiro NOGI 3f732384c4 20250807
20250807
2025-08-07 16:05:51 +09:00

77 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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(/<sheetProtection[\s\S]*?\/>/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;
}
// 削除したいタグ名を指定(例: <workbookProtection ... />
const tagPattern = /<workbookProtection[\s\S]*?\/>/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 が見つかりません。');
}