Skip to content

$fn.csvToObject(csv)

Converts the specified CSV file data to object format.

Arguments

NameTypeDescription
csvBufferBuffer object in CSV file

CSV File Specifications

HeaderRequired
This is the name of the object’s property.
Only single-byte alphanumeric characters are allowed.
(Starting with a numeral is not acceptable.) Example)“email”, “name”
Delimiter,(comma)
FieldIf the value contains double quotes,commas, or line breaks, be sure to enclose them.
Newline CharacterCRLF or LF
EncodingUTF-8
BOMWithout BOM or with BOM

Return value

TypeDescription
array of objectConverts CSV file data to object format and returns it

Sample

try {
/* CSV Contents
* ----------------------------------------------------------------------------
* Id,Title,Description,Progress,Category,CreatedBy,CreatedAt
* 00001,Title 1,"Description 1, Sample 1",100,CATEGORY1,User1,2023-08-01
* 00002,Title 2," Description 2, Sample 2",80,CATEGORY2,User2,2023-08-10
* 00003,Title 3," Description 3, Sample 3",20,CATEGORY3,User3,2023-08-12
* ----------------------------------------------------------------------------
*/
const file = await $fn.getFile("/path/to/projects.csv");
const buffer = file.data;
// Parses CSV files and converts them to objects
const projects = $fn.csvToObject(buffer);
// Register data one case at a time
for (const project of projects) {
const { Id, Title, Description, Category, Progress, CreatedBy, CreatedAt } = project;
if (!Id) {
throw new Error("Please enter ID.");
}
const progress = Number(Progress);
if (isNaN(progress)) {
throw new Error("Enter the percentage of progress with numeric.");
}
/* SQL Function Content
* ------------------------------------------------------------------------------------
* insert into Project VALUES (${param.id},${param.title},${param.description},
* ${param.progress},${param.category},${param.createdBy},${param.createdAt});
* ------------------------------------------------------------------------------------
*/
// Register data in tables with SQL functions
await insertToProjectTable({
id: Id,
title: Title,
description: Description,
progress,
category: Category,
createdBy: CreatedBy,
createdAt: CreatedAt
});
}
} catch (e) {
console.error(e);
}