Spreadsheet API tools in PDF.co help in working with Web APIs which are helpful for data extraction and manipulation. This tutorial and the sample source code explain the working of spreadsheet API tools. For example, they can convert XLS to HTML, XLS to TXT, XLS to CSV, and provide many other such functionalities. This article explains converting an XLS file to a TXT format using the XLS/XLSX to TXT API endpoint of PDF.co web API and JavaScript programming language. The Users can also use these spreadsheet API tools to work with databases and other conversions.
- Features of XLS/XLSX to TXT API Endpoint
- Endpoint Parameters to Extract Excel Data
- How to Extract Excel using JavaScript
- Step By Step Guide to Convert XLS to TXT
- Convert XLS to TXT – Video Guide
Features of XLS/XLSX to TXT API Endpoint
The PDF.co Web API provides various tools and functionalities which come in handy while working with data extraction and manipulation. The exciting thing about this web API is that it helps preserve the original data structure. For example, when the API converts the XLS file into a TXT format, all table values keep their real places. The API places these values in their exact rows and columns.
The API endpoint has various other features, such as preserving and reproducing the source file’s original layout, allowing conversion to portable and lightweight file formats, supporting multiple languages, and automated platform integration. The users can efficiently convert any excel data file into a text format using this API endpoint.
Another essential feature of PDF.co Web API is the security it provides to consumers. The API supports transmitting all the user documents through encrypted connections. The users can learn more about the PDF.co security protocols here.
Endpoint Parameters to Extract Excel Data
Following are the parameters of the XLS/XLSX to TXT API endpoint:
1. URL
It is a required parameter and contains a URL to the source file. The users can upload files from various locations such as Dropbox, Google Drive, and built-in storage form PDF.co file storage. The users can also upload files from the API. Moreover, they can encrypt and decrypt the data files using user-controlled data encryption.
2. Httpusername
It is an optional parameter. It contains HTTP auth user name if it is necessary to access the source URL.
3. httppassword
It is an optional parameter. It contains the HTTP auth password if it is compulsory to access the source URL.
4. name
It is an optional parameter that must be a string. It contains the name of the output file.
5. expiration
It is an optional parameter representing the output link’s expiration time. The default expiration time is 60 minutes, and after this time, the generated output files will vanish from the temporary storage of PDF.co.
6. worksheetindex
It is an optional parameter containing the worksheet index that users must utilize.
7. inline
It is an optional parameter which is a boolean. The users can set the parameter as true to return the data as inline or false to return the link to the output file.
8. async
It is an optional parameter which is boolean. It helps in running the processes asynchronously. This parameter returns the JobId, allowing users to check the process state. Moreover, the possible process states include working, failed, success, and aborted.
9. profiles
It is an optional parameter that helps set additional configurations for extra options and fine-tuning. This parameter has to be in the string format.
Note: If the users keep getting errors like “ Too many requests” and “ Access Denied” for their input URL, they can try adding “cache:” to enable the built-in URL caching.
How to Extract Excel using JavaScript
The following source code explains to the users how to convert an excel format data file to a text format using XLS to TXT API endpoint of the PDF.co Web API. The following sample code in Javascript explains how to use the source data file and prepare the request for the API endpoint. The source file contains sample data with some rows and columns to provide a structure to the data file.
The users must provide the necessary variables and parameters to provide the source URL and request the endpoint to generate the TXT format according to the original layout. The code generates the output file, and the users can download it to check and verify the required optimal conversion.
Source File for Excel Extraction in JavaScript
Following is the source file, which contains sample data for testing:
Source Code for Excel Extraction in JavaScript
Following is the sample code to convert the XLS data file into a TXT file:
var https = require("https"); const API_KEY = "**************************"; // Direct URL of source Excel file. const SourceFileUrl = "https://docs.google.com/spreadsheets/d/1oD0wbYJnbpaPL8Ie-v-f105hfvKpgY0jujbChEC6XrM/edit?usp=sharing"; const load = "true"; const async1 = "true"; const searchString = "Javascript"; const regexSearch = "true"; // Prepare request to `Excel to text` API endpoint var queryPath = `/v1/xls/convert/to/txt`; // JSON payload for api request var jsonPayload = JSON.stringify({ url: SourceFileUrl, }); var reqOptions = { host: "api.pdf.co", method: "POST", path: queryPath, headers: { "x-api-key": API_KEY, "Content-Type": "application/json", "Content-Length": Buffer.byteLength(jsonPayload, "utf8"), }, }; // Send request var postRequest = https .request(reqOptions, (response) => { response.on("data", (d) => { // Parse JSON response var data = JSON.parse(d); if (data.error == false) { console.log(data.url); } else { // Service reported error console.log(data.message); } }); // console.log(response) }) .on("error", (e) => { // Request error console.log(e); }); // Write request data postRequest.write(jsonPayload); postRequest.end();
Output of Extracted Excel
The following screenshot explains the generation of the output file after the code execution. The users can download the output text file on their local storage using the output link and javascript’s file writing modules to write the data in a text file and save it on their computer.
Output File
The following screenshot is of the output text file. The screenshot explains the structure preservation and verifies the data placements in respective rows and columns:
Step-By-Step Guide to Convert XLS to TXT
Following is the step-by-step guide which explains the working of the above sample code:
- The users need to import all necessary packages for API requests. Here, the “require” parameter represents all the necessary modules. On the other hand, “request” is an essential Node.js module that is required for making an HTTP call. In this scenario, the required package is “path”.
- The next step is to provide the API key to access the PDF.co Web API. There is a limitation to sending the direct request, so the users must provide their specific API key to use it as an access token for the authentication.
- To convert them into text, users must declare and initiate the variables containing the source URL, async process, search string, and regex string. The query path contains relevant API endpoint information. The user has to set the async variable as true to run the process asynchronously for smooth working.
- The next step is to provide API options, including the URL of the API endpoint, the request method, the queryPath, and all the required headers. For this purpose, the users must use the abovementioned “reqOptions” to initialize these variables.
- The final step is to send the POST request to the required API endpoint and verify the API response. If the response status is 200, it represents a successful request, and the code execution provides an output URL for the user. On the other hand, if the response is unsuccessful, the users have to modify and execute the code again to obtain the output file.
Convert XLS to TXT – Video Guide
Below is the Gif representing the execution of the working code: