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 workspacelet 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 workspacelet new_price =100output['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 workspaceapi.files().forEach((file) =>console.log(file))// Timestamp information of a file, such as its creation time, modification time, and last access timeconsole.log(api.stat('price.txt'))// Read the text file named 'price.txt' in the workspaceconsole.log(api.read('price.txt'))// Write a text file to the workspaceapi.write('time.txt',moment().format('HH:mm:ss'))// Modify filenames in the workspaceapi.rename('time.txt','currentTime.txt')// Delete files in the workspaceapi.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"3awaitapi.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 recordslet files =awaitapi.splitCSV('large.csv',',',0,'small.csv',5000)// Store the written files, such as ['small(0).csv', 'small(1).csv', ...], into a list of filesawaitapi.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 consolelet list =awaitapi.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' } } ]}awaitapi.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.ziplet inputZip =api.zip()awaitinputZip.load('input.zip')let filesInZip =awaitinputZip.files()filesInZip.forEach((file) =>console.log(file))// Extract the file 'a.txt' from input.zipawaitinputZip.extract('a.txt')// Compress 'b.jpg' and 'c.pdf' from the workspace into 'output.zip'let outputZip =api.zip()awaitoutputZip.add('b.jpg')awaitoutputZip.add('c.pdf')awaitoutputZip.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! 🤖️⚡️