Skip to content

$ps.file.export

Overview

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

Prerequisites

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

Syntax

$ps.file.export(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 Export parameters

Return Value

Returns true if successful. Returns false if it fails.

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 exports a CSV file in the web server and outputs the results to the log.

(a) Export a table with the standard export settings.

JavaScript
const siteId = 100;
const param = {
};
const result = $ps.file.export('01_develop', 'parts/01_parts.csv', siteId, $ps.JSON.stringify(param));
context.Log(result);

(b) Character code: Shift-JIS example

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

© Export a table using the "Export" settings already created in Pleasanter

JavaScript
const siteId = 100;
const param = {
    ExportId: 1
};
const result = $ps.file.export('01_develop', 'parts/01_parts.csv', siteId, $ps.JSON.stringify(param));
context.Log(result);

(d) Export a table by writing directly

JavaScript
const siteId = 100;
const param = {
    Export: {
        Columns: [
            {
                ColumnName: 'ClassA'
            },
            {
                ColumnName: 'UpdatedTime'
            }
        ],
        Header: false,
        Type: 'csv'
    }
};
const result = $ps.file.export('01_develop', 'parts/01_parts.csv', siteId, $ps.JSON.stringify(param));
context.Log(result);

Code Samples

Modify 【 ... 】 in the code as necessary.
1. Export table data to a given folder

This is a sample code that exports table data to a given folder.

The processing flow is as follows.
(1) Move the previously exported CSV files stored in the send folder to backup/send
(2) Store the exported CSV file in the send folder

JavaScript
// Settings
const SECTION = 'files';
const SEND = 'send';
const BACKUP_SEND = 'backup/send';
const SITE_NAME = '【Site name】';
const FILE_DATE_FORMAT = 'utilities';
// Get the processing date and time as a string
function getDateTimeString() {
    return new Date()
        .toLocaleString('ja-JP', {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            hour: '2-digit',
            minute: '2-digit',
            second: '2-digit',
            hour12: false, // 24-hour format
        })
        .replaceAll('/', '')
        .replaceAll(':', '')
        .replaceAll(' ', '');
}
// 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;
// Move the files in the send folder to the backup folder
const lists = $ps.file.getFileList(SECTION, SEND);
if (lists.length > 0) {
    for (const element of lists) {
        const moveResult = $ps.file.moveFile(
            SECTION,
            `${SEND}/${element}`,
            `${BACKUP_SEND}/${element}`,
        );
        if (!moveResult) {
            logs.LogException(`File move failed File name: ${element}`);
            return false;
        } else {
            logs.LogInfo(`File move succeeded File name: ${element}`);
        }
    }
}
// File export: uses the export definition with ID 1. The target records are those whose "target month is this year"
const param = {
    ExportId: 1,
    View: {
        ColumnFilterHash: {
            DateA: '["ThisYear"]',
        },
    },
};
const path = `${SEND}/${FILE_DATE_FORMAT}${getDateTimeString()}.csv`;
const exportResult = $ps.file.export(
    SECTION,
    path,
    siteId,
    $ps.JSON.stringify(param),
);
if (!exportResult) {
    logs.LogException(`Export failed. File name: ${path}`);
    return false;
} else {
    logs.LogInfo(`Export succeeded File name: ${path}`);
}

Supported Versions

Supported versions Body
1.4.13.0 and later Added function