Unfortunately, in the current stage, when the values are retrieved with spreadsheets.values.get
, your situation is obtained as the default situation. In your sample Spreadsheet, it becomes [["One","Two","Three"],["One","Two"],["One","Two","Three"]]
. So in this case, it is required to prepare a script for achieving your goal. In this answer, I would like to propose the script.
Sample script:
// Use spreadsheets.values.get. Please replace this for your actual script.
$res = $service->spreadsheets_values->get($spreadsheetId, $range);
// I added below script.
$values = $res['values'];
$maxLen = max(array_map('count', $values));
foreach ($values as $i => $v) {
$len = count($v);
if ($len < $maxLen) {
$values[$i] = array_merge($v, array_fill(0, $maxLen - $len, ''));
}
}
In this modification, ""
is added for the row when the length of the row is less than the maximum length of the array.
When your sample values are used, in this modification, $values
retrieved by $service->spreadsheets_values->get($spreadsheetId, $range)
, it's [["One","Two","Three"],["One","Two"],["One","Two","Three"]]
. And, when my additional script is used, $values
becomes [["One","Two","Three"],["One","Two",""],["One","Two","Three"]]
.
Reference: