0

I'm using this script to download all tabs in a Google spreadsheet.

When I output the rows being downloaded, it's including blank arrays:

Output: Data 1: [[id, name], [1.0, John], [2.0, Smith], [, ], [, ], [, ], [, ], [, ], [, ], ...

function convertRangeToCsvFile_(csvFileName, sheet) {
  // get available data range in the spreadsheet
  var activeRange = sheet.getDataRange();
  try {
    var data = activeRange.getValues();
    Logger.log('Data 1: %s', data); <----- Logging here

How can I clear this out from the final downloaded csv?

enter image description here

2 Answers 2

1

You can try this. This code filters and returns an array with the sub arrays of length greater than 1

var data = activeRange.getValues();
const filteredData = data.filter(item => item.length > 1);
Sign up to request clarification or add additional context in comments.

Comments

1

Question: why are you using %s to log it? Given that what you are logging is actually an array it might cause some issues.

It seems that in the script you've shown they perform some kind of filtering between row and columns and they parse it into a string later on, I suggest you try something like this:

const data = activeRange.getValues();
const filteredData = data.filter((item) => {
  // We will prevent non array elements that could break the your csv grid here
  if (!Array.isArray(item)){
    return false
  }
  // We will prevent rows with funny values (or no values) here
  for (var column = 0; column < item.length; column++) {
    if(!item[column]) {
      return false;
    }
  }
  // Add any extra condition before this return statement which basically means they are good to go
  return true;
});

And then apply the proper parsing to string or whatever you need to pass on to your csv file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.