Skip to content

$ps.file.import

Overview

Used when importing using "$ps.file" in "Server Script".

Prerequisites

  1. DisableServerScriptFile in "Script.json" must be set to false.
  2. "Import permission" is required for "Table".

Syntax

$ps.file.import(section, path, siteId, json);

Parameters

Parameter Type Required Description
section string Yes Section name. For details on sections, see "About Sections" in "$ps.file".
path string Yes File name. For both Windows and Linux, use "/" as the directory separator.
siteId string Yes Site ID
json string Yes Import parameters

Return Value

Returns the number of new registrations and the number of updates if successful. Returns null if unsuccessful.

Exceptions

If an exception occurs in C#, the server script will raise an exception by putting the exception class name and error message into an Error object.

Usage Examples

The following example imports a CSV file from the web server and outputs the results to the log.

JavaScript
const siteId = 100;
const param = {
};
const result = $ps.file.import('01_develop', 'parts/01_parts.csv', siteId, $ps.JSON.stringify(param));
context.Log($ps.JSON.stringify(result));
Example output
{insertCount:10,updateCount:0}

Character code: Shift-JIS example

const siteId = 100;
const param = {
    Encoding: 'Shift-JIS'
};
const result = $ps.file.import('01_develop', 'parts/01_parts.csv', siteId, $ps.JSON.stringify(param));
context.Log($ps.JSON.stringify(result));

Example output
{insertCount:10,updateCount:10}

Example of updating records with matching keys

const siteId = 100;
const param = {
    UpdatableImport: true,
    Key: 'ClassA'
};
const result = $ps.file.import('01_develop', 'parts/01_parts.csv', siteId, $ps.JSON.stringify(param));
context.Log($ps.JSON.stringify(result));

Output example
{insertCount:10,updateCount:10}

Code Samples

Modify 【 ... 】 in the code as necessary.
1. Import CSV files stored in a given folder

This is a sample code that imports CSV files stored in a given folder.

The processing flow is as follows.
(1) Import the CSV files stored in the received folder into Pleasanter
(2) After processing, move them to backup/received

JavaScript
const SECTION = 'files';
const RECEIVED = 'received';
const BACKUP_RECEIVED = 'backup/received';
const SITE_NAME = '【Site name】';
// Get the site information
const site = items.GetClosestSite(SITE_NAME);
if (!site) {
    logs.LogException(`Site: ${SITE_NAME} was not found.`);
    return false;
}
const siteId = site.SiteId;
// Read the files in the received folder and import them
const param = {};
const lists = $ps.file.getFileList(SECTION, RECEIVED);
if (lists.length > 0) {
    for (const element of lists) {
        const result = $ps.file.import(
            SECTION,
            `${RECEIVED}/${element}`,
            siteId,
            $ps.JSON.stringify(param),
        );
        if (!result) {
            logs.LogException(
                `Import failed. File name: ${element}`,
            );
            return false;
        } else {
            logs.LogInfo(
                `Import succeeded File name: ${element} Result: ${$ps.JSON.stringify(
                    result,
                )}`,
            );
        }
    }
    // After the import is complete, move the files to the backup folder
    for (const element of lists) {
        const moveResult = $ps.file.moveFile(
            SECTION,
            `${RECEIVED}/${element}`,
            `${BACKUP_RECEIVED}/${element}`,
        );
        if (!moveResult) {
            logs.LogException(`File move failed File name: ${element}`);
            return false;
        } else {
            logs.LogInfo(`File move succeeded File name: ${element}`);
        }
    }
}

Supported Versions

Supported versions Body
1.4.13.0 and later Added function