How could I export shift-jis encoded csv?

Hi there.

In Japan, the common encoding of csv file is shift-jis.

I need shift-jis encoded csv file to register shipping data to shipping company in Japan.
Not limited to this case, there are many situations in Japan where shift-jis format CSV files are required.

I couldn't come up with an idea to achieve this on Retool, so I use my own API to generate a CSV and return a download link.
I want to achieve this with Retool.

Below is an example of exporting shift-jis encoded csv (not fit to Retool).

<body>
   <a id="download" href="#" download="data.csv" target="_blank">download</a>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.3.7/papaparse.js"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/encoding-japanese/1.0.28/encoding.min.js"></script>
   <script>
      const csv = Papa.unparse(values)
      
      // convert csv into shift-jis array with encoding.js
      const codeArr = Encoding.convert(csv, {
        from: 'UNICODE',
        to: 'SJIS',
        type: 'array'
      })
        
      // array to blob
      const uint8Array = new Uint8Array(codeArr)
      const blob = new Blob([ uint8Array ], { type: 'text/csv' })
      
      // set a download link to element
      window.URL = window.URL || window.webkitURL
      document.getElementById("download").href = window.URL.createObjectURL(blob);
   </script>
</body>

Is there any idea to export shift-jis encoded csv on Retool?

Thank you.

Hi Reona, thanks for raising this. There's indeed a way to download a generated file in retool. You can create a JavaScript query, then use utils.downloadFile(blob, "filename", "csv") to generate a file and trigger a download action.



You can also add third party libraries to retool via the top right [...] menu -> scripts and styles. Note that however, not all third party libraries are supported. Papaparse is already included in retool, but it looks like we don't currently support the encoding library that you use. Is there an alternative that you can try?


Sorry for my late reply.
After getting the answer from you, I faced some problems, but in the end I was able to find a way to do this!
I made the first argument a BASE64 string and it worked.

The code below is working correctly.

const csv = Papa.unparse( csvArray );
const utf8Array = new TextEncoder().encode( csvUnparsed );
const sjisArray = Encoding.convert(utf8Array, 'SJIS', 'UTF8');
const BASE64_STRING = Encoding.base64Encode(sjisArray);
console.log("BASE64_STRING: " + BASE64_STRING)
utils.downloadFile({base64Binary: BASE64_STRING}, "data_sjis", "csv");

Thank you for your support!