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;
}
add a comment |
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;
}
If each row is named by their ID, you can use getRangeByName
– Thum Choon Tat
Nov 8 at 8:13
add a comment |
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;
}
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;
}
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
add a comment |
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
add a comment |
active
oldest
votes
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%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
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
If each row is named by their ID, you can use getRangeByName
– Thum Choon Tat
Nov 8 at 8:13