Created
April 7, 2025 19:16
-
-
Save curioshiki/6892f08e1f732084fead4421cb2dfe40 to your computer and use it in GitHub Desktop.
Extract All Comments in Excel,using OfficeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function main(workbook: ExcelScript.Workbook) { | |
| // ブック内の全シートをループ | |
| let worksheets = workbook.getWorksheets(); | |
| worksheets.forEach(sh => { | |
| try { | |
| let comments = sh.getComments(); | |
| // コメント出力用シート 同名シートがすでにあれば削除 | |
| let shexName = sh.getName() + "_comments"; | |
| let existingSheet = workbook.getWorksheet(shexName); | |
| if (existingSheet) { | |
| existingSheet.delete(); | |
| } | |
| // コメント出力用シート作成 | |
| let shex = workbook.addWorksheet(shexName); | |
| shex.getRange("A1").setValue("セル"); | |
| shex.getRange("B1").setValue("作成者"); | |
| shex.getRange("C1").setValue("内容"); | |
| let exLine = 2; | |
| // シート内の全コメントをループ | |
| comments.forEach(c => { | |
| shex.getRange(`A${exLine}`).setValue(c.getLocation().getAddress()); | |
| shex.getRange(`B${exLine}`).setValue(c.getAuthorName()); | |
| shex.getRange(`C${exLine}`).setValue(c.getContent()); | |
| exLine++; | |
| // コメントに返信があれば、そのコメントについた全返信をループ | |
| let replies = c.getReplies(); | |
| if (replies.length > 0) { | |
| replies.forEach(rp => { | |
| shex.getRange(`A${exLine}`).setValue("返信"); | |
| shex.getRange(`B${exLine}`).setValue(rp.getAuthorName()); | |
| shex.getRange(`C${exLine}`).setValue(rp.getContent()); | |
| exLine++; | |
| }); | |
| } | |
| }); | |
| } catch (error) { | |
| //既存のコメント用シートを削除した結果、コレクション内に実在しないワークシートが含まれてしまい、getComments()がエラーになるので、もしエラーが出ても気にしない | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment