How to handle empty cells when splitting data in Google Sheets

I’m working with inventory data in Google Sheets and need help creating a formula that can properly handle empty values when splitting cells.

My current dataset has product information with columns for ID numbers, names, batch codes, and testing details. The testing columns contain multiple values separated by line breaks, but some cells are completely empty which is causing issues.

I want to transform this data so each test type and requirement appears on its own row, paired with the corresponding product details.

Here’s what I’ve tried so far:

=LET(
  product_id; FLATTEN(A2:A7 & B2:B7 & C2:C7);
  product_name; FLATTEN(D2:D7);
  batch_number; FLATTEN(E2:E7);
  
  category_1; SPLIT(FLATTEN(F2:F7); CHAR(10));
  requirement_1; SPLIT(FLATTEN(G2:G7); CHAR(10));
  category_2; SPLIT(FLATTEN(H2:H7); CHAR(10));
  requirement_2; SPLIT(FLATTEN(I2:I7); CHAR(10));
  
  FILTER(
    HSTACK(product_id; product_name; batch_number; category_1; requirement_1; category_2; requirement_2);
    category_1 <> ""
  )
)

The problem is that this formula doesn’t work correctly when some cells are null or empty. It throws errors and doesn’t align the data properly across rows. How can I modify this approach to handle empty cells better while still splitting the multi-line content into separate rows?

FLATTEN breaks row associations when you’ve got empty cells in your source data. I hit this same issue with manufacturing data - some products had missing specs and everything got misaligned. Here’s what fixed it for me: pre-process with TRANSPOSE and BYROW to handle each original row separately, then combine the results. Don’t flatten everything upfront. Use REDUCE to iterate through each row and build your output step by step. For empty cells, throw in COALESCE or wrap it in IF statements to check if cells are blank before trying to split them. This keeps your product info and test data properly aligned even when entire testing columns are empty.

wrap your SPLIT functions with IFNA or IFERROR. empy cells mess up the alignment since SPLIT returns different array sizes. try IFNA(SPLIT(cell; CHAR(10)); "") to keep everythin aligned when there’s nothing to split.

This happens because Google Sheets can’t handle arrays with different sizes when you FLATTEN and SPLIT cells that have varying numbers of line breaks. The mismatched array sizes break your HSTACK operation.

I hit this same issue with survey data last year. You need to normalize your arrays before combining them. Don’t split everything at once - process each row individually with BYROW or MAP functions. This keeps each product’s data together even when some testing columns are empty.

Another option: use SEQUENCE to create a master index based on the max number of splits in any row, then use INDEX to pull the corresponding values. This keeps everything aligned even when some cells have fewer values. The trick is getting a consistent row structure before you try to stack arrays horizontally.