I have function, gatherAds
which is attached to a button in a variety of worksheets in a Google Sheets workbook. It calls an ADS
function which runs a library routine that talks to some middleware on our server to pull down Google Ads data. The result
variable contains a 2D array which is injected into the workbook at row 9, column 2 (after clearing out anything that's already there.)
At the bottom the actual .setValues()
is in a try/catch. That's because, even though it works, it still insists on throwing an error, namely: The number of columns in the data does not match the number of columns in the range. The data has 1 but the range has 15.
(or variations on that theme.)
Why does something that works also throw an error? And what's wrong with worksheet.getRange(9, 2, length, width).setValues(result);
in any case?
function gatherAds() {
const workbook = SpreadsheetApp.getActive();
const worksheet = workbook.getActiveSheet();
const B = [];
for (let i = 0; i < 7; i++) {
B.push(worksheet.getRange('B' + (i + 1)).getDisplayValue());
}
const result = ADS(...B);
const length = result.length;
const width = result[0].length;
const range = worksheet.getRange(9, 2, worksheet.getLastRow(), worksheet.getLastColumn());
range.clear();
SpreadsheetApp.flush();
try {
worksheet.getRange(9, 2, length, width).setValues(result);
} catch (E) {
console.error(E.message);
}
}
/**
* ADS Calls GoogleAds
* @param {string} customerId
* @param {string} selectClauseFields
* @param {string} fromClause
* @param {string} whereClause
* @param {string} orderByClause
* @param {string} limitClause
* @param {string} parametersClause
* @customfunction
*/
function ADS(customerId, selectClauseFields, fromClause, whereClause, orderByClause, limitClause, parametersClause) {
return DaisyChain.ADS3(customerId, selectClauseFields, fromClause, whereClause, orderByClause, limitClause, parametersClause);
}
ADS()
always returns a 2D array whose rows are all the same length?result
before setting the value? I'm suspecting that the value ofresult
is sometimes empty 2D array which causes the errorThe number of columns in the data does not match the number of columns in the range. The data has 1 but the range has 15.
setValues
with a ragged array.