Skip to content

Instantly share code, notes, and snippets.

@robomatic
Last active June 10, 2020 17:11
Show Gist options
  • Save robomatic/3cae9db2fa393df508762f9fec87377f to your computer and use it in GitHub Desktop.
Save robomatic/3cae9db2fa393df508762f9fec87377f to your computer and use it in GitHub Desktop.
Google Sheets Split Cell CSV Values to Rows
// select the 1 or more rows to set the active range
function cellCSVSplitToRows() {
const sheet = SpreadsheetApp.getActiveSheet()
const aRange = sheet.getActiveRange()
let rowTracker = 0
function setFeatures(range) {
const startIndex = range.getRowIndex()
// target the column with CSV content here
const fColIndex = 3
const features = range.getCell(1,fColIndex).getValue().split(',').map(f => [f])
const rows = features.length
const destRange = sheet.getRange(startIndex + 1, fColIndex, rows, 1)
const featureColumnRange = sheet.getRange(startIndex + 1, fColIndex, rows, 1)
sheet.insertRowsAfter(startIndex, rows)
destRange.setValues(features)
rowTracker += (rows + 1)
}
const arEnd = aRange.getLastRow()
const arStart = aRange.getRowIndex()
for(let i = arStart; i <= arEnd; i++) {
let currentStart = arStart + rowTracker
let r = sheet.getRange('Sheet1!A'+ currentStart + ':W' + currentStart)
setFeatures(r)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment