NetSuite file save examples #3058

Closed
opened 2024-01-17 22:34:53 +00:00 by ben-ekw · 1 comment

In case it helps other NetSuite users, here's an example Suitelet that can save an Excel file to the NetSuite File Cabinet or prompt the user to download the Excel file:

/**
 * @NApiVersion 2.1
 * @NAmdConfig ./JsLibraryConfig.json
 * @NScriptType Suitelet
 */
define(["N/file", "xlsx"], function (file, XLSX) {
  async function onRequest(context) {
    const workbook = XLSX.utils.book_new();
    let rows, worksheet;

    rows = [
      { name: "George Washington", birthday: "1732-02-22" },
      { name: "John Adams", birthday: "1735-10-19" },
    ];
    worksheet = XLSX.utils.json_to_sheet(rows);
    XLSX.utils.book_append_sheet(workbook, worksheet, "Birthdays");

    rows = [
      { name: "Apple", color: "red" },
      { name: "Banana", color: "yellow" },
    ];
    worksheet = XLSX.utils.json_to_sheet(rows);
    XLSX.utils.book_append_sheet(workbook, worksheet, "Fruit");

    /*
    // write XLSX workbook to NetSuite File Cabinet
    let content = await XLSX.write(workbook, {
      bookType: "xlsx",
      type: "base64",
    });
    let newfile = file.create({
      name: "test.xlsx", // replace with desired file name
      fileType: file.Type.EXCEL,
      contents: content,
    });
    newfile.folder = 12345; // replace with internal ID of desired File Cabinet folder
    let newfileId = newfile.save();
    context.response.write("Saved File ID: " + newfileId);
    */

    // prompt user for file download
    context.response.setHeader(
      "Content-Type",
      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    );
    context.response.setHeader(
      "Content-Disposition",
      "attachment; filename=" + "test.xlsx" // replace with desired file name
    );
    await context.response.write(
      XLSX.write(workbook, { bookType: "xlsx", type: "base64" })
    );
  }
  return { onRequest: onRequest };
});

In case it helps other NetSuite users, here's an example Suitelet that can save an Excel file to the NetSuite File Cabinet or prompt the user to download the Excel file: ``` /** * @NApiVersion 2.1 * @NAmdConfig ./JsLibraryConfig.json * @NScriptType Suitelet */ define(["N/file", "xlsx"], function (file, XLSX) { async function onRequest(context) { const workbook = XLSX.utils.book_new(); let rows, worksheet; rows = [ { name: "George Washington", birthday: "1732-02-22" }, { name: "John Adams", birthday: "1735-10-19" }, ]; worksheet = XLSX.utils.json_to_sheet(rows); XLSX.utils.book_append_sheet(workbook, worksheet, "Birthdays"); rows = [ { name: "Apple", color: "red" }, { name: "Banana", color: "yellow" }, ]; worksheet = XLSX.utils.json_to_sheet(rows); XLSX.utils.book_append_sheet(workbook, worksheet, "Fruit"); /* // write XLSX workbook to NetSuite File Cabinet let content = await XLSX.write(workbook, { bookType: "xlsx", type: "base64", }); let newfile = file.create({ name: "test.xlsx", // replace with desired file name fileType: file.Type.EXCEL, contents: content, }); newfile.folder = 12345; // replace with internal ID of desired File Cabinet folder let newfileId = newfile.save(); context.response.write("Saved File ID: " + newfileId); */ // prompt user for file download context.response.setHeader( "Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ); context.response.setHeader( "Content-Disposition", "attachment; filename=" + "test.xlsx" // replace with desired file name ); await context.response.write( XLSX.write(workbook, { bookType: "xlsx", type: "base64" }) ); } return { onRequest: onRequest }; }); ```
Author

Here's another example combining CSV data into a multi-tabbed XLSX file:

/**
 * @NApiVersion 2.1
 * @NAmdConfig ./JsLibraryConfig.json
 * @NScriptType Suitelet
 */
define(["N/file", "xlsx"], function (file, XLSX) {
  async function onRequest(context) {
    const workbook = XLSX.utils.book_new();
    let rows, worksheet;

    rows = `"Name","Birthday"
"George Washington","1732-02-22"
"John Adams","1735-10-19"`;
    worksheet = await XLSX.read(rows, { type: "string" }).Sheets.Sheet1;
    XLSX.utils.book_append_sheet(workbook, worksheet, "Birthdays");

    rows = `"Name","Color"
"Apple","red"
"Banana","yellow"`;
    worksheet = await XLSX.read(rows, { type: "string" }).Sheets.Sheet1;
    XLSX.utils.book_append_sheet(workbook, worksheet, "Fruit");

    /*
    // write XLSX workbook to NetSuite File Cabinet
    let content = await XLSX.write(workbook, {
      bookType: "xlsx",
      type: "base64",
    });
    let newfile = file.create({
      name: "test.xlsx", // replace with desired file name
      fileType: file.Type.EXCEL,
      contents: content,
    });
    newfile.folder = 12345; // replace with internal ID of desired File Cabinet folder
    let newfileId = newfile.save();
    context.response.write("Saved File ID: " + newfileId);
    */

    // prompt user for file download
    context.response.setHeader(
      "Content-Type",
      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    );
    context.response.setHeader(
      "Content-Disposition",
      "attachment; filename=" + "test.xlsx" // replace with desired file name
    );
    await context.response.write(
      XLSX.write(workbook, { bookType: "xlsx", type: "base64" })
    );
  }
  return { onRequest: onRequest };
});
Here's another example combining CSV data into a multi-tabbed XLSX file: ``` /** * @NApiVersion 2.1 * @NAmdConfig ./JsLibraryConfig.json * @NScriptType Suitelet */ define(["N/file", "xlsx"], function (file, XLSX) { async function onRequest(context) { const workbook = XLSX.utils.book_new(); let rows, worksheet; rows = `"Name","Birthday" "George Washington","1732-02-22" "John Adams","1735-10-19"`; worksheet = await XLSX.read(rows, { type: "string" }).Sheets.Sheet1; XLSX.utils.book_append_sheet(workbook, worksheet, "Birthdays"); rows = `"Name","Color" "Apple","red" "Banana","yellow"`; worksheet = await XLSX.read(rows, { type: "string" }).Sheets.Sheet1; XLSX.utils.book_append_sheet(workbook, worksheet, "Fruit"); /* // write XLSX workbook to NetSuite File Cabinet let content = await XLSX.write(workbook, { bookType: "xlsx", type: "base64", }); let newfile = file.create({ name: "test.xlsx", // replace with desired file name fileType: file.Type.EXCEL, contents: content, }); newfile.folder = 12345; // replace with internal ID of desired File Cabinet folder let newfileId = newfile.save(); context.response.write("Saved File ID: " + newfileId); */ // prompt user for file download context.response.setHeader( "Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ); context.response.setHeader( "Content-Disposition", "attachment; filename=" + "test.xlsx" // replace with desired file name ); await context.response.write( XLSX.write(workbook, { bookType: "xlsx", type: "base64" }) ); } return { onRequest: onRequest }; }); ```
sheetjs pinned this 2024-01-26 23:21:33 +00:00
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#3058
No description provided.