Workspace Script

Execute Low-Code in the workspace to access text files within the folder

Input Object

In the script, you can directly access the input object. Each key in the input object corresponds to a .txt file name (excluding the file extension) in the workspace and each value represents the text content of that file. When the script is executed, all .txt files are automatically loaded, saving the need to read them manually in the script:

// Read the text content of "price.txt" in the workspace
let price = input['price']
console.log('price:', price)

Output Object

When the script needs to output multiple text files, you can directly modify the output object. Each key-value pair added to the output object will be output as a .txt file in the workspace, with the file name being the key and the text content being the corresponding value for that key.

//write text to 'price.txt' in the workspace
let new_price = 100
output['new_price'] = new_price

API

Provides synchronous functions: api.files(), api.stat(filename), api.read(filename, encoding='utf8'), api.write(filename, text), api.rename(oldname, newname), and api .remove(filename). Additionally, built-in libraries such as moment.js and lodash.js can be used directly:

// List all files in the workspace
api.files().forEach((file) => console.log(file))

// Timestamp information of a file, such as its creation time, modification time, and last access time
console.log(api.stat('price.txt'))

// Read the text file named 'price.txt' in the workspace
console.log(api.read('price.txt'))

// Write a text file to the workspace
api.write('time.txt', moment().format('HH:mm:ss'))

// Modify filenames in the workspace
api.rename('time.txt', 'currentTime.txt')

// Delete files in the workspace
api.remove('price.txt')

And asynchronous functions that return a Promise object: api.run(uuid), api.readCSV(filename, separator, skiplines), api.splitCSV(filename, separator, skiplines, newname, maxRows), api.writeCSV(filename, rows, header), and api.writeXML(filename, obj, options):

// Call and execute a specific automation skill with the identifier "ef4e12a6-636f-4e58-8e14-212089e6fd23"3
await api.run('ef4e12a6-636f-4e58-8e14-212089e6fd23')

// Split a large CSV format file in the workspace into multiple smaller CSV format files, with each file containing no more than 5000 records
let files = await api.splitCSV('large.csv', ',', 0, 'small.csv', 5000)

// Store the written files, such as ['small(0).csv', 'small(1).csv', ...], into a list of files
await api.writeCSV('list.csv', files.map((file, index) => {return {file, index}}), ['index','file'])

// Read the CSV files in the working folder and print the data to the console
let list = await api.readCSV('list.csv')
console.log(list) 

// Convert a JSON object into XML format, add attributes for <addr>, output a header, indent the XML, and output to a file named 'result.xml'
let obj = {
  root: [
    { date: '2023/03/12' },
    { item: 'coffee' },
    { item: 'tea' },
    {
      _name: 'addr',
      _content: '101F, #1, Taiwan Road',
      _attrs: {
        country: 'TWN',
        city: 'TPE'
      }
    }
  ]
}
await api.writeXML('result.xml', obj, { header: true, indent: '\t' })

Additionally, zip read and write APIs are provided for compressing or decompressing files in the working folder. These APIs are asynchronous functions as well:

// Read the files within input.zip
let inputZip = api.zip()
await inputZip.load('input.zip')
let filesInZip = await inputZip.files()
filesInZip.forEach((file) => console.log(file))

// Extract the file 'a.txt' from input.zip
await inputZip.extract('a.txt')

// Compress 'b.jpg' and 'c.pdf' from the workspace into 'output.zip'
let outputZip = api.zip()
await outputZip.add('b.jpg')
await outputZip.add('c.pdf')
await outputZip.save('output.zip')

--

We are dedicated to improving our content. Please let us know if you come across any errors, including spelling, grammar or other mistakes, as your feedback is valuable to us! 🤖️⚡️

Last updated