Is it possible to get the URL for google sheet in .dsv format?

Viewed 83

I've just discovered the possibility to export a google sheet in csv format but I actually need the separator to be a "|" and not a "," Is there any way to do that?

2 Answers

Try this (You will probably have to encapsulate the data with quotes.). Here the file is stored in 'test' folder (to be adapted to your project).

const where = 'test' // here the name of folder where you want to record the file
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('* Export ... *')
    .addItem('... to .dsv format !', 'createDSVFile')
    .addToUi();
}
// you have to declare a trigger on onEdit
function onEdit(event){
  var sh = event.source.getActiveSheet();
  if (sh.getName()=='mySheet'){
    createDSVFile()
  }
}
function createDSVFile() {
  var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lr=sh.getLastRow();
  var lc=sh.getLastColumn();
  var sep = '|';
  var txt = '';
  for (var i=1;i<=lr;i++){
    for (var j=1;j<=lc;j++){
      txt += sh.getRange(i,j).getValue() + sep
    }
    txt = txt.substring(0,txt.length-1) + '\n'
  }
  var folders = DriveApp.getFoldersByName(where);
  if (folders.hasNext()) {
    var folder = folders.next();
    saveData(folder, 'myDSVFile.dsv',txt);
  }
}
function saveData(folder, fileName, content) {
  var children = folder.getFilesByName(fileName);
  var file = null;
  if (children.hasNext()) {
    file = children.next();
    file.setContent(content);
  } else {
    file = folder.createFile(fileName, content);
  }
  toast('File "' + fileName + '" recorded !')
}
function toast(body, title, timeout) {
  return SpreadsheetApp.getActive().toast(
    body,
    title || "information",
    timeout || 5 // In seconds
  );
}

https://docs.google.com/spreadsheets/d/1h1xXLEOScw2duqydJwtBvZcsfO2vHI05WOf3fojC6VI/copy

Related