better way to return a row based on cell value in google apps script











up vote
0
down vote

favorite












Is there a better way to find the row based on cell value without looping through each row? The function findRowById loop through each row of the sheet and search the row if the first column is equal to the search string "id".



var ss = SpreadsheetApp.openById("******************");
var sheet = ss.getSheetByName('Sheet1');

function doGet(e) {
if(e.parameter.action == "find" && e.parameter.id != undefined)
return findRowById(ss,e.parameter.id);
}

function findRowById(sheetname,id) {

var dataRange = sheetname.getDataRange();
var values = dataRange.getValues();
var result = {};
var data = ;
data.push(values[0]);

for (var i = 1; i < values.length; i++) {
if(values[i][0] == id){
data.push(values[i]);
result.records = getJsonArrayFromData(data);
}
}

return ContentService.createTextOutput(JSON.stringify(result))
.setMimeType(ContentService.MimeType.JSON)
}

function getJsonArrayFromData(data)
{

var obj = {};
var result = ;
var headers = data[0];
var cols = headers.length;
var row = ;

for (var i = 1, l = data.length; i < l; i++)
{
// get a row to fill the object
row = data[i];
// clear object
obj = {};
for (var col = 0; col < cols; col++)
{
// fill object with new values
obj[headers[col]] = row[col];
}
// add object in a final result
result.push(obj);
}

return result;

}









share|improve this question






















  • If each row is named by their ID, you can use getRangeByName
    – Thum Choon Tat
    Nov 8 at 8:13















up vote
0
down vote

favorite












Is there a better way to find the row based on cell value without looping through each row? The function findRowById loop through each row of the sheet and search the row if the first column is equal to the search string "id".



var ss = SpreadsheetApp.openById("******************");
var sheet = ss.getSheetByName('Sheet1');

function doGet(e) {
if(e.parameter.action == "find" && e.parameter.id != undefined)
return findRowById(ss,e.parameter.id);
}

function findRowById(sheetname,id) {

var dataRange = sheetname.getDataRange();
var values = dataRange.getValues();
var result = {};
var data = ;
data.push(values[0]);

for (var i = 1; i < values.length; i++) {
if(values[i][0] == id){
data.push(values[i]);
result.records = getJsonArrayFromData(data);
}
}

return ContentService.createTextOutput(JSON.stringify(result))
.setMimeType(ContentService.MimeType.JSON)
}

function getJsonArrayFromData(data)
{

var obj = {};
var result = ;
var headers = data[0];
var cols = headers.length;
var row = ;

for (var i = 1, l = data.length; i < l; i++)
{
// get a row to fill the object
row = data[i];
// clear object
obj = {};
for (var col = 0; col < cols; col++)
{
// fill object with new values
obj[headers[col]] = row[col];
}
// add object in a final result
result.push(obj);
}

return result;

}









share|improve this question






















  • If each row is named by their ID, you can use getRangeByName
    – Thum Choon Tat
    Nov 8 at 8:13













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Is there a better way to find the row based on cell value without looping through each row? The function findRowById loop through each row of the sheet and search the row if the first column is equal to the search string "id".



var ss = SpreadsheetApp.openById("******************");
var sheet = ss.getSheetByName('Sheet1');

function doGet(e) {
if(e.parameter.action == "find" && e.parameter.id != undefined)
return findRowById(ss,e.parameter.id);
}

function findRowById(sheetname,id) {

var dataRange = sheetname.getDataRange();
var values = dataRange.getValues();
var result = {};
var data = ;
data.push(values[0]);

for (var i = 1; i < values.length; i++) {
if(values[i][0] == id){
data.push(values[i]);
result.records = getJsonArrayFromData(data);
}
}

return ContentService.createTextOutput(JSON.stringify(result))
.setMimeType(ContentService.MimeType.JSON)
}

function getJsonArrayFromData(data)
{

var obj = {};
var result = ;
var headers = data[0];
var cols = headers.length;
var row = ;

for (var i = 1, l = data.length; i < l; i++)
{
// get a row to fill the object
row = data[i];
// clear object
obj = {};
for (var col = 0; col < cols; col++)
{
// fill object with new values
obj[headers[col]] = row[col];
}
// add object in a final result
result.push(obj);
}

return result;

}









share|improve this question













Is there a better way to find the row based on cell value without looping through each row? The function findRowById loop through each row of the sheet and search the row if the first column is equal to the search string "id".



var ss = SpreadsheetApp.openById("******************");
var sheet = ss.getSheetByName('Sheet1');

function doGet(e) {
if(e.parameter.action == "find" && e.parameter.id != undefined)
return findRowById(ss,e.parameter.id);
}

function findRowById(sheetname,id) {

var dataRange = sheetname.getDataRange();
var values = dataRange.getValues();
var result = {};
var data = ;
data.push(values[0]);

for (var i = 1; i < values.length; i++) {
if(values[i][0] == id){
data.push(values[i]);
result.records = getJsonArrayFromData(data);
}
}

return ContentService.createTextOutput(JSON.stringify(result))
.setMimeType(ContentService.MimeType.JSON)
}

function getJsonArrayFromData(data)
{

var obj = {};
var result = ;
var headers = data[0];
var cols = headers.length;
var row = ;

for (var i = 1, l = data.length; i < l; i++)
{
// get a row to fill the object
row = data[i];
// clear object
obj = {};
for (var col = 0; col < cols; col++)
{
// fill object with new values
obj[headers[col]] = row[col];
}
// add object in a final result
result.push(obj);
}

return result;

}






google-apps-script






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 2:24









Juan Coder

193




193












  • If each row is named by their ID, you can use getRangeByName
    – Thum Choon Tat
    Nov 8 at 8:13


















  • If each row is named by their ID, you can use getRangeByName
    – Thum Choon Tat
    Nov 8 at 8:13
















If each row is named by their ID, you can use getRangeByName
– Thum Choon Tat
Nov 8 at 8:13




If each row is named by their ID, you can use getRangeByName
– Thum Choon Tat
Nov 8 at 8:13

















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',
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53200679%2fbetter-way-to-return-a-row-based-on-cell-value-in-google-apps-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53200679%2fbetter-way-to-return-a-row-based-on-cell-value-in-google-apps-script%23new-answer', 'question_page');
}
);

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







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings