Data Snippets
Data snippets allow you to manipulate data and ensure that it's format meets your requirements.
# Get the date and time with JavaScript
Get the date X days in the past.
const yesterday = new Date()
yesterday.setDate(yesterday.getDate() - 1) // Change '1' to your desired number of days in the past
return [[yesterday.toLocaleDateString('en-US')]]
Get the date X days in the future.
const targetDate = new Date()
targetDate.setDate(targetDate.getDate() + 14) // Change '14' to your desired number of days in the future
return [[targetDate.toLocaleDateString('en-US')]]
Append timestamp to scraped data.
let data = [all-interaction-data] // Token from previous scrape step
let dt = new Date().toLocaleString("en-GB", { timeZone: 'Europe/London' })
for (var i = 0; i < data.length; i++) {
data[i].push([dt])
}
return data
For more information on locales, see Mozilla | Intl (opens new window).
# Get the current day of the week
Return the day of the week in the locale that is passed to it.
const locale = "en-US";
const options = { weekday: "long" };
const now = new Date();
return new Intl.DateTimeFormat(locale, options).format(now);
For more information on locales, see Mozilla | Intl (opens new window).
# Adding a constant to scrape data
Blank cells in data can appear when scraping an inconsistent data source and the scraper could not find data. When passing this to data storage options, like Google Sheets, the Google Sheets API will ignore these and cause some data to shift to the left. For example, the following object: [["1", "2"]["", "4"]] will cause the Google Sheet to look like this:
| A | B |
|---|---|
| 1 | 2 |
| 4 |
To ensure that this does not happen, a constant value can be added to the data, for example:
var data = [scrape-data];
const placeholder = '-';
for (var i = 0 ; i < data.length ; i++) {
for (var j = 0 ; j < data[i].length ; j++) {
if (data[i][j] == "") {
data[i][j] = placeholder;
}
}
}
return data;
The placeholder variable may be replaced to suit requirements. The code-data data token can be used to write the data to a data storage solution. The resulting data will be written as:
| A | B |
|---|---|
| 1 | 2 |
| - | 4 |
# Adding a constant value to the last column
To add a constant value to the last column of your data, you will need to employ some custom JavaScript. First, gather the data that you wish to write to a Google Sheet - this could be data that you have scraped, for example. Use the following code to add a value to the last column, where [scrape-data] is the variable that contains the information you want to append to and data is the variable that holds your value, this could also be a variable.
var scarpeData = [scrape-data];
var data = 'example'
scrapeData.forEach((item) => item.push(data));
return scrapeData;
# Creating and downloading a CSV file with JavaScript
To create and download a CSV file with JavaScript, you will need to use the following code. This accepts a token that contains the data that you wish to include as a file. We would recommend reviewing the export to CSV step before attempting to use the code below.
// Update me by removing the [google-sheet-data] and replacing this using the "insert data" option to find the token that contains the data that you wish to export
const data = [google-sheet-data]
function arrayToCSV(array) {
return array.map(row =>
row.map(value => {
if (typeof value === 'string' && (value.includes(',') || value.includes('"'))) {
return `"${value.replace(/"/g, '""')}"`;
}
return value;
}).join(',')
).join('\n');
}
function writeCSVFile(directory, filename, csvData) {
const fullPath = `${directory}/${filename}`
fs.writeFile(fullPath, csvData, 'utf8', (err) => {
if (err) {
console.error('Error writing CSV file:', err);
} else {
console.log(`CSV file saved as ${fullPath}`);
}
});
}
const csvData = arrayToCSV(data);
// Change me to the desired download path, preserving the forward slashes
const downloadDirectory = 'C:/Users/Axiom/Documents/Axiom';
// [optional] the name of the final output
const filename = 'output.csv';
writeCSVFile(downloadDirectory, filename, csvData);
# Manipulating data with regex
Regular expressions (regex) are patterns used to match, search, and manipulate text based on specific rules, making them powerful for data validation, extraction, and transformation. It can be used within your Javascript code to manipulate data within your automations.
In the following examples, we will assume that you have a list of data that you are using within a "Loop through data" step to ensure only one string is being manipulated. If you do not wish to use a "Loop through data" step, you can build a loop into your code instead.
# Extracting data
To extract data using regex, you can use the match function. For example:
// Let's use data scraped from a website, this contains the string "115 Records".
const data = '[scrape-data?all&0]';
// We just want the number of records here, let's extract it with regex.
const records = data.match(/\+d/)[0];
// Return this to be included in the 'code-data' data token.
return records;
# Replacing data
To replace data using regex, you can use the replace function. For example:
// Let's use data scraped from a website, this contains the string "axiom@example.com".
const data = '[scrape-data?all&0]';
// We want to replace the domain with a new domain.
const newEmail = data.replace(/@[\w.-]+$/, "@newdomain.com");
// Return this to be included in the 'code-data' data token.
return newEmail;
# Validating data
To validate data using regex, you can use the test function. For example:
// Let's use data scraped from a website, this contains the string "example@example.com".
const data = '[scrape-data?all&0]';
// We want to confirm the email address is valid.
const isValid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(data);
// Return this to be included in the 'code-data' data token.
return isValid;
# Reformatting data
To reformat data using regex, you can use the replace function. For example:
// Let's use data scraped from a website, this contains the string "01-02-2025".
const data = '[scrape-data?all&0]';
// We want to change this to be formatted as "01/02/2025".
const formatted = data.replace(/(\d{4})-(\d{2})-(\d{2})/, "$1/$2/$3");
// Return this to be included in the 'code-data' data token.
return formatted;