How to fix column reference within a multidimensional array for custom SUMIF function?
I have created a custom function in GAS that is equivalent to a SUMIF formula and will run through an array that contains a range of data within a google sheet. The function will sum the 'capacity' column when the 'status' column matches entries from a defined range in the sheet.
It worked fine until I rearranged the array to be separated by columns rather than by row. The columns are arranged: Project name, Connection type, Ownership, Country, Capacity, Status, Technology, Fuel, Commissioning date, Commissioning end date. This is how the array looks now, it is sorted by the columns.
[[Azambi Hydro, Budana Hydro, Busanga Hydro, Dilolo Thermal, N'zilo I Hydro], [Embedded, Embedded, Embedded, Embedded, Embedded], [IPP, IPP, IPP, IPP, PPP], [DR Congo, DR Congo, DR Congo, DR Congo, DR Congo], [10.0, 11.0, 240.0, 0.0, 108.0], [Construction, Operating, Planned, Operating, Rehabilitation], [Conventional hydro, Conventional hydro, Conventional hydro, Undecided or unknown, Conventional hydro], [Hydro, Hydro, Hydro, Cogeneration, Hydro], [Sat Dec 01 08:00:00 GMT+00:00 2018, Mon Jan 01 08:00:00 GMT+00:00 1940, Fri Dec 31 08:00:00 GMT+00:00 2021, , Thu Jan 01 08:00:00 GMT+00:00 1953], [, , , , ]]
For some reason the function isn't working with the array arranged this way and I can't seem to figure out why.
Full code:
var sheet = SpreadsheetApp.getActiveSpreadsheet()
var statusColumn = sheet.getSheetByName('Supply Projections').getRange(6,7,9,1).getValues()
var status = statusColumn.map(function (x) { return x[0] });
var status0 = status[0];
var status1 = status[1];
var status2 = status[2];
var status3 = status[3];
var status4 = status[4];
var status5 = status[5];
var status6 = status[6];
var status7 = status[7];
var status8 = status[8];
var statusArray = [status0,status1,status2,status3,status4,status5,status6,status7,status8];
function transpose(data) {
return (data[0] || ).map (function (col , colIndex) {
return data.map (function (row) {
return row[colIndex];
});
});
}
var sheetData = transpose(sheet.getSheetByName('Project
Selector').getRange('$D$3:M7').getValues())
//SUMIF function
function sumIf(sumColIdx, criteriaColIdx, criteria, data) {
return data
.filter(function (row) {
return row[criteriaColIdx] === criteria
})
.reduce(function (acc, row) {
return acc + row[sumColIdx]
}, 0)
}
var capacityStatusSums = statusArray.map(function (criteria) {
var statusColIdx = 5
var capacityColIdx = 4
return sumIf(capacityColIdx, statusColIdx, criteria, sheetData)
})
I think it might be the way I am referencing the position within the array, see statusColIdx = 5 and capacityColIdx = 4. Previously, they referenced the 5th and 4th column within my data range and I assumed it would be the same with the array arranged by columns but I am not sure.
javascript multidimensional-array google-apps-script google-sheets
add a comment |
I have created a custom function in GAS that is equivalent to a SUMIF formula and will run through an array that contains a range of data within a google sheet. The function will sum the 'capacity' column when the 'status' column matches entries from a defined range in the sheet.
It worked fine until I rearranged the array to be separated by columns rather than by row. The columns are arranged: Project name, Connection type, Ownership, Country, Capacity, Status, Technology, Fuel, Commissioning date, Commissioning end date. This is how the array looks now, it is sorted by the columns.
[[Azambi Hydro, Budana Hydro, Busanga Hydro, Dilolo Thermal, N'zilo I Hydro], [Embedded, Embedded, Embedded, Embedded, Embedded], [IPP, IPP, IPP, IPP, PPP], [DR Congo, DR Congo, DR Congo, DR Congo, DR Congo], [10.0, 11.0, 240.0, 0.0, 108.0], [Construction, Operating, Planned, Operating, Rehabilitation], [Conventional hydro, Conventional hydro, Conventional hydro, Undecided or unknown, Conventional hydro], [Hydro, Hydro, Hydro, Cogeneration, Hydro], [Sat Dec 01 08:00:00 GMT+00:00 2018, Mon Jan 01 08:00:00 GMT+00:00 1940, Fri Dec 31 08:00:00 GMT+00:00 2021, , Thu Jan 01 08:00:00 GMT+00:00 1953], [, , , , ]]
For some reason the function isn't working with the array arranged this way and I can't seem to figure out why.
Full code:
var sheet = SpreadsheetApp.getActiveSpreadsheet()
var statusColumn = sheet.getSheetByName('Supply Projections').getRange(6,7,9,1).getValues()
var status = statusColumn.map(function (x) { return x[0] });
var status0 = status[0];
var status1 = status[1];
var status2 = status[2];
var status3 = status[3];
var status4 = status[4];
var status5 = status[5];
var status6 = status[6];
var status7 = status[7];
var status8 = status[8];
var statusArray = [status0,status1,status2,status3,status4,status5,status6,status7,status8];
function transpose(data) {
return (data[0] || ).map (function (col , colIndex) {
return data.map (function (row) {
return row[colIndex];
});
});
}
var sheetData = transpose(sheet.getSheetByName('Project
Selector').getRange('$D$3:M7').getValues())
//SUMIF function
function sumIf(sumColIdx, criteriaColIdx, criteria, data) {
return data
.filter(function (row) {
return row[criteriaColIdx] === criteria
})
.reduce(function (acc, row) {
return acc + row[sumColIdx]
}, 0)
}
var capacityStatusSums = statusArray.map(function (criteria) {
var statusColIdx = 5
var capacityColIdx = 4
return sumIf(capacityColIdx, statusColIdx, criteria, sheetData)
})
I think it might be the way I am referencing the position within the array, see statusColIdx = 5 and capacityColIdx = 4. Previously, they referenced the 5th and 4th column within my data range and I assumed it would be the same with the array arranged by columns but I am not sure.
javascript multidimensional-array google-apps-script google-sheets
Not sure what your intention is with the code outside the SUMIF? If the sumif is a custom function it is meant to be used as a (custom) formula in the spreadsheet, right?
– JPV
Dec 27 at 16:01
Well my idea is to do all the formulas within the script. Since this will be shared with multiple people, I figured it would be safer not to have formulas floating in the sheet.
– Ajay Ubhi
Dec 27 at 16:34
Since we are missing a lot of background (e.g: where is statusArray defined?), all I can say is that colIdx 4 refers to column H in your data and colIdx would thus be column I.
– JPV
Dec 27 at 16:41
Sorry, I have edited the post to show how 'statusArray' is defined.
– Ajay Ubhi
Dec 27 at 16:53
add a comment |
I have created a custom function in GAS that is equivalent to a SUMIF formula and will run through an array that contains a range of data within a google sheet. The function will sum the 'capacity' column when the 'status' column matches entries from a defined range in the sheet.
It worked fine until I rearranged the array to be separated by columns rather than by row. The columns are arranged: Project name, Connection type, Ownership, Country, Capacity, Status, Technology, Fuel, Commissioning date, Commissioning end date. This is how the array looks now, it is sorted by the columns.
[[Azambi Hydro, Budana Hydro, Busanga Hydro, Dilolo Thermal, N'zilo I Hydro], [Embedded, Embedded, Embedded, Embedded, Embedded], [IPP, IPP, IPP, IPP, PPP], [DR Congo, DR Congo, DR Congo, DR Congo, DR Congo], [10.0, 11.0, 240.0, 0.0, 108.0], [Construction, Operating, Planned, Operating, Rehabilitation], [Conventional hydro, Conventional hydro, Conventional hydro, Undecided or unknown, Conventional hydro], [Hydro, Hydro, Hydro, Cogeneration, Hydro], [Sat Dec 01 08:00:00 GMT+00:00 2018, Mon Jan 01 08:00:00 GMT+00:00 1940, Fri Dec 31 08:00:00 GMT+00:00 2021, , Thu Jan 01 08:00:00 GMT+00:00 1953], [, , , , ]]
For some reason the function isn't working with the array arranged this way and I can't seem to figure out why.
Full code:
var sheet = SpreadsheetApp.getActiveSpreadsheet()
var statusColumn = sheet.getSheetByName('Supply Projections').getRange(6,7,9,1).getValues()
var status = statusColumn.map(function (x) { return x[0] });
var status0 = status[0];
var status1 = status[1];
var status2 = status[2];
var status3 = status[3];
var status4 = status[4];
var status5 = status[5];
var status6 = status[6];
var status7 = status[7];
var status8 = status[8];
var statusArray = [status0,status1,status2,status3,status4,status5,status6,status7,status8];
function transpose(data) {
return (data[0] || ).map (function (col , colIndex) {
return data.map (function (row) {
return row[colIndex];
});
});
}
var sheetData = transpose(sheet.getSheetByName('Project
Selector').getRange('$D$3:M7').getValues())
//SUMIF function
function sumIf(sumColIdx, criteriaColIdx, criteria, data) {
return data
.filter(function (row) {
return row[criteriaColIdx] === criteria
})
.reduce(function (acc, row) {
return acc + row[sumColIdx]
}, 0)
}
var capacityStatusSums = statusArray.map(function (criteria) {
var statusColIdx = 5
var capacityColIdx = 4
return sumIf(capacityColIdx, statusColIdx, criteria, sheetData)
})
I think it might be the way I am referencing the position within the array, see statusColIdx = 5 and capacityColIdx = 4. Previously, they referenced the 5th and 4th column within my data range and I assumed it would be the same with the array arranged by columns but I am not sure.
javascript multidimensional-array google-apps-script google-sheets
I have created a custom function in GAS that is equivalent to a SUMIF formula and will run through an array that contains a range of data within a google sheet. The function will sum the 'capacity' column when the 'status' column matches entries from a defined range in the sheet.
It worked fine until I rearranged the array to be separated by columns rather than by row. The columns are arranged: Project name, Connection type, Ownership, Country, Capacity, Status, Technology, Fuel, Commissioning date, Commissioning end date. This is how the array looks now, it is sorted by the columns.
[[Azambi Hydro, Budana Hydro, Busanga Hydro, Dilolo Thermal, N'zilo I Hydro], [Embedded, Embedded, Embedded, Embedded, Embedded], [IPP, IPP, IPP, IPP, PPP], [DR Congo, DR Congo, DR Congo, DR Congo, DR Congo], [10.0, 11.0, 240.0, 0.0, 108.0], [Construction, Operating, Planned, Operating, Rehabilitation], [Conventional hydro, Conventional hydro, Conventional hydro, Undecided or unknown, Conventional hydro], [Hydro, Hydro, Hydro, Cogeneration, Hydro], [Sat Dec 01 08:00:00 GMT+00:00 2018, Mon Jan 01 08:00:00 GMT+00:00 1940, Fri Dec 31 08:00:00 GMT+00:00 2021, , Thu Jan 01 08:00:00 GMT+00:00 1953], [, , , , ]]
For some reason the function isn't working with the array arranged this way and I can't seem to figure out why.
Full code:
var sheet = SpreadsheetApp.getActiveSpreadsheet()
var statusColumn = sheet.getSheetByName('Supply Projections').getRange(6,7,9,1).getValues()
var status = statusColumn.map(function (x) { return x[0] });
var status0 = status[0];
var status1 = status[1];
var status2 = status[2];
var status3 = status[3];
var status4 = status[4];
var status5 = status[5];
var status6 = status[6];
var status7 = status[7];
var status8 = status[8];
var statusArray = [status0,status1,status2,status3,status4,status5,status6,status7,status8];
function transpose(data) {
return (data[0] || ).map (function (col , colIndex) {
return data.map (function (row) {
return row[colIndex];
});
});
}
var sheetData = transpose(sheet.getSheetByName('Project
Selector').getRange('$D$3:M7').getValues())
//SUMIF function
function sumIf(sumColIdx, criteriaColIdx, criteria, data) {
return data
.filter(function (row) {
return row[criteriaColIdx] === criteria
})
.reduce(function (acc, row) {
return acc + row[sumColIdx]
}, 0)
}
var capacityStatusSums = statusArray.map(function (criteria) {
var statusColIdx = 5
var capacityColIdx = 4
return sumIf(capacityColIdx, statusColIdx, criteria, sheetData)
})
I think it might be the way I am referencing the position within the array, see statusColIdx = 5 and capacityColIdx = 4. Previously, they referenced the 5th and 4th column within my data range and I assumed it would be the same with the array arranged by columns but I am not sure.
javascript multidimensional-array google-apps-script google-sheets
javascript multidimensional-array google-apps-script google-sheets
edited Dec 27 at 16:52
asked Dec 27 at 14:19
Ajay Ubhi
52
52
Not sure what your intention is with the code outside the SUMIF? If the sumif is a custom function it is meant to be used as a (custom) formula in the spreadsheet, right?
– JPV
Dec 27 at 16:01
Well my idea is to do all the formulas within the script. Since this will be shared with multiple people, I figured it would be safer not to have formulas floating in the sheet.
– Ajay Ubhi
Dec 27 at 16:34
Since we are missing a lot of background (e.g: where is statusArray defined?), all I can say is that colIdx 4 refers to column H in your data and colIdx would thus be column I.
– JPV
Dec 27 at 16:41
Sorry, I have edited the post to show how 'statusArray' is defined.
– Ajay Ubhi
Dec 27 at 16:53
add a comment |
Not sure what your intention is with the code outside the SUMIF? If the sumif is a custom function it is meant to be used as a (custom) formula in the spreadsheet, right?
– JPV
Dec 27 at 16:01
Well my idea is to do all the formulas within the script. Since this will be shared with multiple people, I figured it would be safer not to have formulas floating in the sheet.
– Ajay Ubhi
Dec 27 at 16:34
Since we are missing a lot of background (e.g: where is statusArray defined?), all I can say is that colIdx 4 refers to column H in your data and colIdx would thus be column I.
– JPV
Dec 27 at 16:41
Sorry, I have edited the post to show how 'statusArray' is defined.
– Ajay Ubhi
Dec 27 at 16:53
Not sure what your intention is with the code outside the SUMIF? If the sumif is a custom function it is meant to be used as a (custom) formula in the spreadsheet, right?
– JPV
Dec 27 at 16:01
Not sure what your intention is with the code outside the SUMIF? If the sumif is a custom function it is meant to be used as a (custom) formula in the spreadsheet, right?
– JPV
Dec 27 at 16:01
Well my idea is to do all the formulas within the script. Since this will be shared with multiple people, I figured it would be safer not to have formulas floating in the sheet.
– Ajay Ubhi
Dec 27 at 16:34
Well my idea is to do all the formulas within the script. Since this will be shared with multiple people, I figured it would be safer not to have formulas floating in the sheet.
– Ajay Ubhi
Dec 27 at 16:34
Since we are missing a lot of background (e.g: where is statusArray defined?), all I can say is that colIdx 4 refers to column H in your data and colIdx would thus be column I.
– JPV
Dec 27 at 16:41
Since we are missing a lot of background (e.g: where is statusArray defined?), all I can say is that colIdx 4 refers to column H in your data and colIdx would thus be column I.
– JPV
Dec 27 at 16:41
Sorry, I have edited the post to show how 'statusArray' is defined.
– Ajay Ubhi
Dec 27 at 16:53
Sorry, I have edited the post to show how 'statusArray' is defined.
– Ajay Ubhi
Dec 27 at 16:53
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53946523%2fhow-to-fix-column-reference-within-a-multidimensional-array-for-custom-sumif-fun%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53946523%2fhow-to-fix-column-reference-within-a-multidimensional-array-for-custom-sumif-fun%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Not sure what your intention is with the code outside the SUMIF? If the sumif is a custom function it is meant to be used as a (custom) formula in the spreadsheet, right?
– JPV
Dec 27 at 16:01
Well my idea is to do all the formulas within the script. Since this will be shared with multiple people, I figured it would be safer not to have formulas floating in the sheet.
– Ajay Ubhi
Dec 27 at 16:34
Since we are missing a lot of background (e.g: where is statusArray defined?), all I can say is that colIdx 4 refers to column H in your data and colIdx would thus be column I.
– JPV
Dec 27 at 16:41
Sorry, I have edited the post to show how 'statusArray' is defined.
– Ajay Ubhi
Dec 27 at 16:53