Export vers un fichier csv

This commit is contained in:
Vincent Vandemeulebrouck 2024-09-25 23:38:25 +02:00
parent 7e8f642d87
commit b6a203b82a

View File

@ -25,10 +25,17 @@ export class ExportScriptarium {
condition: (target) => game.user.isGM && condition: (target) => game.user.isGM &&
OptionsAvancees.isUsing(EXPORT_CSV_SCRIPTARIUM) && OptionsAvancees.isUsing(EXPORT_CSV_SCRIPTARIUM) &&
this.$getActors(actorDirectory, target).length > 0, this.$getActors(actorDirectory, target).length > 0,
callback: target => this.exportActors(this.$getActors(actorDirectory, target)) callback: target => this.exportActors(this.$getActors(actorDirectory, target), this.$getTargetName(actorDirectory, target))
}) })
} }
$getTargetName(actorDirectory, target) {
const li = target.closest(".directory-item")
const folderId = li.data("folderId")
const actorId = li.data("documentId")
return actorId
? game.actors.get(actorId).name
: actorDirectory.folders.find(it => it.id == folderId).name
}
$getActors(actorDirectory, target) { $getActors(actorDirectory, target) {
const li = target.closest(".directory-item") const li = target.closest(".directory-item")
const folderId = li.data("folderId") const folderId = li.data("folderId")
@ -41,14 +48,13 @@ export class ExportScriptarium {
return actors.filter(it => it.type == ACTOR_TYPES.personnage) return actors.filter(it => it.type == ACTOR_TYPES.personnage)
} }
exportActors(actors) { exportActors(actors, targetName) {
console.log(LOG_HEAD + 'exportActors', actors) const eol = '\n\r'
const header = Misc.join(this.getHeaderLine(), ';') const header = Misc.join(this.getHeaderLine(), ';')
console.log(header) const actorLines = actors.map(actor => Misc.join(this.getActorLine(actor), ';'))
actors.forEach(actor => { const data = Misc.join([header, ...actorLines], eol)
const actorLine = Misc.join(this.getActorLine(actor), ';') const filename = `scriptatium-${targetName?.slugify()}.csv`;
console.log(actorLine) saveDataToFile(data, "text/csv", `${filename}`);
})
} }
getHeaderLine() { getHeaderLine() {
@ -69,5 +75,4 @@ export class ExportScriptarium {
} }
return it return it
} }
} }