Last active
August 26, 2020 21:12
-
-
Save GastonZalba/78327bd9290ec37a9aa1251a565a3e81 to your computer and use it in GitHub Desktop.
Transform comma separated strings cell values to PostgresSQL arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const SHEET_NAME = '+para_DB'; | |
const TARGET_COLUMN_NAME = 'categoria'; | |
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName( SHEET_NAME ); | |
const data = sheet.getDataRange().getValues(); | |
let column = data[0].indexOf(TARGET_COLUMN_NAME); | |
// Transform comma separated strings cell values to PostgresSQL arrays | |
function transformDataToArray() { | |
for ( let i = 1; i < data.length; i++ ) { | |
if ( data[i][column] !== '' ) { | |
let arr = data[i][column].split(','); | |
arr = Array.isArray(arr) ? arr : [arr]; | |
arr = arr.map(s => s.trim()); | |
arr = JSON.stringify(arr); | |
arr = arr.replace('[', '{').replace(']', '}'); | |
sheet.getRange( i+1, column+1 ).setValue(arr); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment