I have trouble parsing strings in the cells of Google Sheets using Google Apps Script. I used the JavaScript method Array.indexOf
, but failed to find a character that's present in the string in a cell. I tried to insert underscores between the letters of the string in the cells, but only one underscore was inserted at the beginning of the string in each cell.
Here is my code:
function testCSV() {
const ss = SpreadsheetApp.getActiveSpreadsheet()
const sheet = ss.getSheets()[0];
const range = sheet.getDataRange();
const values = range.getValues();
let csvStr = "";
Logger.log(values);
for (let i = 0; i < values.length; i++) {
let row = "";
for (let j = 0; j < values[i].length; j++) {
if (values[i][j]) {
row = row + "_" + values[i][j];
}
row = row + ",";
}
row = row.substring(0, (row.length-1));
Logger.log(row);
csvStr += row + '\n';
}
}
This screenshot shows the Logger output.
I want to enclose strings in cells that have commas in them with double quotation marks, just like what shows when CSV files are saved and opened in text format. Does anyone have a solution for this problem?