【Scheduler】How I can create a custom CSV file from a query

# Introduction #
I am creating a CSV file based on a query by using Sharperlight Publisher and Scheduler.
The following two Sharperlight objects are prepared.
+ A Published Query
+ A Scheduled Task

The Published Query collects data which is output to a csv file.
The Scheduled Task does executing the query above, and output the query result to a csv file.

I configure the task to output all values are enclosed in double quotes like this.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/b9d403d6-6087-b748-8ad1-b163750543f5.png)

However, I also would like to amend the output so that all columns on the header row, and the first column on the data rows don't have doble-quote around the value.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/d46b211c-e604-aeb0-f84e-6d2f3b181534.png)

# Published Query #
I create a simple query fetching data from SAP Business One.
The published Query is called as **QiitaSample.PublisherSaveAs**.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/d04a162e-32a8-9171-2597-c96eebcd0af2.png)
Then its query definition is like this.
It gets the first 5 rows from Sales A/R table in SAP Business One database.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/2d091f62-5966-8499-e3d0-5589e55bcff8.png)
Its preview is like this. It's simple isn't it.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/565612ff-7837-0d81-f89a-6c0ff58c647c.png)

# Scheduled Task #
I create a Scheduled Task for outputing the data generated by the query above to a csv file.
The task is called as **QiitaSample.PublisherSaveAs**.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/b64995d1-274b-1d0e-4965-7e53a35b6ce1.png)
There are two actions need to be defined. The first one is for executing the query and save the result as a csv file, and the second one is for editing the contents in the csv file.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/9e91c4a4-fa8c-6428-d21a-18d7d473bfa3.png)

### Action1: PublisherSaveAs ###
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/b3b1466a-11e1-51c8-7d3c-864e30de4513.png)

### Action2: JavaScript ###
I use a JavaScript code to edit the contents of the csv file.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/716d2939-99e5-404d-ce93-0fa8b684beff.png)
```js
RemoveHeaderQuotes(); // Call the function

//Start 
function RemoveHeaderQuotes() {
       try {
              //File Path - Input here
              var filePath = "C:\\QiitaSampleTemp\\TestCSV.csv";
              console.log(filePath);

              if(!lib_sys.io.file.Exists(filePath)){
                     console.error("error", "file doesn't exist");
              }

              //Read file
              var readLines = lib_sys.io.file.ReadAllText(filePath);
              var lines = readLines.split('\n');       // Split into lines
              lines[0] = lines[0].replace(/"/g,'');	// Remove double-quote from the first line. i.e. header row

              // Remove double-quote from the first column of each data row
              for(var l=1; l<lines.length; l++){
                     var cols = lines[l].split(","); 
                     cols[0] = cols[0].replace(/"/g,'');
                     lines[l] = cols.join(",");
              }

              // Put all together back
              var updatedLines = lines.join('\n');
              console.log(updatedLines);

              //Writebck to file
              lib_sys.io.file.WriteAllText(filePath,updatedLines);

       }catch(err){
              console.error("Error: " + err);
       }
}
```

# Verification #
Firstly, I disable the **JavaScript** action and execute the action **PublisherSaveAs** only.
I can see this result and they all are enclosed in double quotes.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/9048283a-bf16-72d6-cda6-149be80e4df1.png)
Then make the **JavaScript** action enabled and execute the task.
The result becomes what I would like to see.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/f4765462-2f66-199d-bef0-75020f7fa0f6.png)


# Conclusion #
Because Sharplight is so flexible, I found I could combine different techniques to find different solutions.