|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "io/fs" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/devlights/goxcel" |
| 12 | +) |
| 13 | + |
| 14 | +type ( |
| 15 | + Args struct { |
| 16 | + dir string |
| 17 | + text string |
| 18 | + onlyHit bool |
| 19 | + verbose bool |
| 20 | + debug bool |
| 21 | + } |
| 22 | +) |
| 23 | + |
| 24 | +var ( |
| 25 | + args Args |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + appLog = log.New(os.Stdout, "", 0) |
| 30 | +) |
| 31 | + |
| 32 | +func init() { |
| 33 | + flag.StringVar(&args.dir, "dir", ".", "directory path") |
| 34 | + flag.StringVar(&args.text, "text", "", "search text") |
| 35 | + flag.BoolVar(&args.onlyHit, "only-hit", true, "show ONLY HIT") |
| 36 | + flag.BoolVar(&args.verbose, "verbose", false, "verbose mode") |
| 37 | + flag.BoolVar(&args.debug, "debug", false, "debug mode") |
| 38 | +} |
| 39 | + |
| 40 | +func abs(p string) string { |
| 41 | + abs, err := filepath.Abs(p) |
| 42 | + if err != nil { |
| 43 | + log.Panic(err) |
| 44 | + } |
| 45 | + |
| 46 | + return abs |
| 47 | +} |
| 48 | + |
| 49 | +func main() { |
| 50 | + log.SetFlags(0) |
| 51 | + flag.Parse() |
| 52 | + |
| 53 | + if args.text == "" { |
| 54 | + flag.PrintDefaults() |
| 55 | + os.Exit(1) |
| 56 | + } |
| 57 | + |
| 58 | + if args.dir == "" { |
| 59 | + args.dir = "." |
| 60 | + } |
| 61 | + |
| 62 | + if err := run(); err != nil { |
| 63 | + log.Fatal(err) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func run() error { |
| 68 | + quit := goxcel.MustInitGoxcel() |
| 69 | + defer quit() |
| 70 | + |
| 71 | + excel, release := goxcel.MustNewGoxcel() |
| 72 | + defer release() |
| 73 | + |
| 74 | + excel.MustSilent(false) |
| 75 | + |
| 76 | + wbs := excel.MustWorkbooks() |
| 77 | + |
| 78 | + rootDir := abs(args.dir) |
| 79 | + err := filepath.WalkDir(rootDir, func(path string, d fs.DirEntry, err error) error { |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + if d.IsDir() { |
| 85 | + return nil |
| 86 | + } |
| 87 | + |
| 88 | + if strings.Contains(filepath.Base(path), "~$") { |
| 89 | + return nil |
| 90 | + } |
| 91 | + |
| 92 | + if !strings.HasSuffix(path, ".xlsx") { |
| 93 | + return nil |
| 94 | + } |
| 95 | + |
| 96 | + absPath := abs(path) |
| 97 | + wb, wsRelease, err := wbs.Open(absPath) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + defer wsRelease() |
| 102 | + |
| 103 | + if args.debug { |
| 104 | + appLog.Printf("Document Open: %s", absPath) |
| 105 | + } |
| 106 | + |
| 107 | + sheets, err := wb.WorkSheets() |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + relPath, _ := filepath.Rel(rootDir, absPath) |
| 113 | + _, err = sheets.Walk(func(ws *goxcel.Worksheet, index int) error { |
| 114 | + rng, err := ws.UsedRange() |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + startCell, err := rng.Cells(1, 1) |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + |
| 124 | + foundRange, found, err := rng.Find(args.text, startCell) |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + |
| 129 | + if !found { |
| 130 | + if !args.onlyHit { |
| 131 | + appLog.Printf("%s: Not Found", relPath) |
| 132 | + } |
| 133 | + |
| 134 | + return nil |
| 135 | + } |
| 136 | + |
| 137 | + name, _ := ws.Name() |
| 138 | + if args.verbose { |
| 139 | + col, _ := foundRange.Column() |
| 140 | + row, _ := foundRange.Row() |
| 141 | + value, _ := foundRange.Value() |
| 142 | + |
| 143 | + appLog.Printf("%s %q (%d,%d): %q", relPath, name, row, col, value) |
| 144 | + } else { |
| 145 | + appLog.Printf("%s: %q", relPath, name) |
| 146 | + return nil |
| 147 | + } |
| 148 | + |
| 149 | + startCell, _ = foundRange.Cells(1, 1) |
| 150 | + for i := 0; found; i++ { |
| 151 | + after, _ := foundRange.Cells(1, 1) |
| 152 | + |
| 153 | + foundRange, found, err = rng.FindNext(after) |
| 154 | + if err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + |
| 158 | + if !found { |
| 159 | + break |
| 160 | + } |
| 161 | + |
| 162 | + if args.debug { |
| 163 | + printCellPos(startCell, after) |
| 164 | + } |
| 165 | + |
| 166 | + if i > 0 && sameCell(startCell, after) { |
| 167 | + break |
| 168 | + } |
| 169 | + |
| 170 | + if args.verbose { |
| 171 | + col, _ := foundRange.Column() |
| 172 | + row, _ := foundRange.Row() |
| 173 | + value, _ := foundRange.Value() |
| 174 | + |
| 175 | + appLog.Printf("%s %q (%d,%d): %q", relPath, name, row, col, value) |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return nil |
| 180 | + }) |
| 181 | + |
| 182 | + if err != nil { |
| 183 | + return err |
| 184 | + } |
| 185 | + |
| 186 | + return nil |
| 187 | + }) |
| 188 | + |
| 189 | + if err != nil { |
| 190 | + return err |
| 191 | + } |
| 192 | + |
| 193 | + return nil |
| 194 | +} |
| 195 | + |
| 196 | +func sameCell(c1, c2 *goxcel.Cell) bool { |
| 197 | + col1, _ := c1.Column() |
| 198 | + row1, _ := c1.Row() |
| 199 | + col2, _ := c2.Column() |
| 200 | + row2, _ := c2.Row() |
| 201 | + |
| 202 | + return col1 == col2 && row1 == row2 |
| 203 | +} |
| 204 | + |
| 205 | +func printCellPos(startCell, after *goxcel.Cell) { |
| 206 | + col1, _ := startCell.Column() |
| 207 | + row1, _ := startCell.Row() |
| 208 | + col2, _ := after.Column() |
| 209 | + row2, _ := after.Row() |
| 210 | + |
| 211 | + appLog.Printf("FindNext: startCell=(%d,%d)\tafter=(%d,%d)", row1, col1, row2, col2) |
| 212 | +} |
0 commit comments