I've written a Google Sheet to SQL automation using WayScript
Tags: javascript, tutorial, wayscript
Published: 2020-05-11 23:11:37

Hey there DEV.to community!

Recently I've got the chance to know a website called [WayScript](https://wayscript.com) providing you a really powerful tool to create your own scripts using a visual environment. WayScript is not like other visual tools! It lets you use JavaScript and Python to continue your programming using them if you like.

So I decided to try it out and I wrote a script which converts a sheet from my Google Sheets to a SQL insert statement.

You can access my script here: [Google Sheet to SQL](https://wayscript.com/user/Adnan%20babakan/20069)

So here is how I've done it. Of course, you need to create an account to create your own script or clone my script.

You should find the `New Script` button pretty easily up on the menu. After clicking on the `New Script` button you will be asked to enter a name for your script, then you are in the right place to start writing your script.

![New Script](https://dev-to-uploads.s3.amazonaws.com/i/0iyyb0dabtkzhesh11x4.png)

Now you can search for the Google Sheet module then drag and drop it in the main function of your script.

![Google sheet module](https://dev-to-uploads.s3.amazonaws.com/i/rnz6lpj58w7kzjtx836w.png)

After adding the module you will see a setting section on the left panel (you can access this section by clicking on the module in the script tree later) which you have to configure it so it can access the sheet you want, here I used a simple sheet (you can check it out [here](https://docs.google.com/spreadsheets/d/1O8hGIjO2weOGcN_K2Wu_cScfhVqcLtdF4miVEOyxEaY)).

![Google sheet module settings](https://dev-to-uploads.s3.amazonaws.com/i/wwaovv5yervwianfxyo7.png)

Click on each `Import Column` so they become available as a variable and can be used later in our program.

Then search for the `Create Variable` module and add it to your script so your script looks like this so far:

![Script tree](https://dev-to-uploads.s3.amazonaws.com/i/0sm81t4c3q5ify76qjlg.png)

Click on the `Create Variable` module you just created so the settings section opens up and configure it to add a new variable to your script called `table_name`.

![Create Variable module settings](https://dev-to-uploads.s3.amazonaws.com/i/eu5xz51aluknrlx26z2h.png)

Next, we are going to dive into programming a little bit deeper using JavaScript as our programming language. WayScript also supports Python and it is only a matter of personal preference and since I like JavaScript more, I decided to write my program using it, you can choose Python if you prefer.

In order to do so add a `JavaScript` module to your script and edit its code and input the code below in it:
```javascript
const keys = []
const recordsCount = variables['Column_0'].length - 1
const values = []

for(let i = 0; i < recordsCount; i++) {
    values[i] = []
}

for(const column in variables) {
    if(typeof variables[column] !== 'object') continue
    const thisColumn = variables[column]

    
    keys.push('\'' + thisColumn[0].toLowerCase().replace(/\s/, '_') + '\'')

    for(let i = 0; i < recordsCount; i++) {
        values[i].push('\'' + thisColumn[i + 1] + '\'')
    }
}

let insertValues = []

for(let i = 0; i < recordsCount; i++) {
    insertValues[i] = '(' + values[i].join(',') + ')'
}

let sqlInsert = 'INSERT INTO ' + variables.table_name + ' (' + keys.join(', ') + ') VALUES ' + insertValues.join(', ') + ';';

console.log(sqlInsert)
```

As you can see, here we used a variable called `variables` which is not defined! The `variables` variable is provided by WayScript itself which contains all the columns we imported and another variable called `table_name` that we created using the `Create Variable` module before. The `variables` variable is an object containing the data added to it from any inputs.

So this JavaScript code simply processes the columns imported from the Google sheet and generates an SQL insert statement, which you can see the output if you run your script.

Your final script tree should look similar to this:

![Final script tree](https://dev-to-uploads.s3.amazonaws.com/i/lcdd3cnr178bg5xuyx47.png)

You can run your script by clicking on the `Run Main` button.

WayScript is way more than only this and you can schedule your script to run at a specific hour during the day and so on.

I hope you enjoyed this and share your experience with WayScript here with me.