Problem selecting rows meeting specific conditions in Goolge-Sheets and NodeJS

My webapp backend is in Node.js, used for reading/writing to/from a Google Sheets spreadsheet.

I want to use a query that will retrieve only rows which meet specific conditions, but haven’t been able to do this.
[Note: I’d like to save on traffic, and fetch only the filtered data rather than fetch everything and filter them locally]

The following method receives a single (string) parameter, customerID.
Ultimately I want retrieve the single row where the cutomerID column matches the supplied parameter, and the status column equals New

(column2==customerID) && (column4==’New’)

Here’s what I have so far:

async function getDraftOrderByID(customerID) {
        try {
            const auth = new GoogleAuth({ scopes: 'https://www.googleapis.com/ auth/spreadsheets.readonly'});
            const sheets = google.sheets({version: 'v4', auth });
            const spreadsheetId = 'MY-SPREADSHEET-ID';
            const range = 'Orders!A3:D9';
            const filter = 'column2 = "cid444"';

            const myRetrievedData = await sheets.spreadsheets.values. get({
                spreadsheetId,
                range,
            });

            console.log('Retrieved from ORDERS tab: ', myRetrievedData.data.values);
        } catch (error) {
            console.error('Error retrieving draft Order ID :((( ', error);
            throw error;
        }

Sample contents of the sheet:

OrderID CustomerID OrderDate Status
X8G3F7 cid222 23FEB2024 Complete
V3B4R3 cid444 18MAR2024 New
E5T2A2 cid9999 18APR2023 Complete
Y5H5J6 cid444 27DEC2023 progress

if method is run for cid444, it should retrieve the contents of row 2, but currently running this method results in returning all rows in range which are 3,4,5:
[
[ ‘V3B4R3’ , ‘cid444′ , ’18MAR2024’ , ‘New’ ]
[ ‘E5T2A2’ , ‘cid9999′ , ’18APR2023’ , ‘Complete’ ]
[ ‘Y5H5J6’ , ‘cid444′ , ’27DEC2023’ , ‘progress’ ]
]

in an attempt to evaluate proposed solutions, I added the “filter” variable as a third parameter to values.get,
also tried using “values.batchGetByDataFilter” and defining a filter criteria JSON. None worked so far.

Mostly, errors would look like:
GaxiosError: Invalid JSON payload received.
Unknown name “filter”: Cannot bind query parameter.
Field ‘filter’ could not be found in request message.

Suggestion:

Since myRetrievedData.data.values already returns a 2D array, you can instead use filter to only return rows with a matching CustomerId and a status of New

I was able to get your desired result by replacing:

console.log('Retrieved from ORDERS tab: ', myRetrievedData.data.values);

with:

console.log('Retrieved from ORDERS tab: ',
  myRetrievedData.data.values.filter(
    row => row[1] == customerID && row[3] == 'New'
  ));

Edit:

Filtering results based on values is currently not possible via Sheets API. As a workaround, You can query using Google Visualization API instead.

Do try this modified version of your script:

function getDraftOrderByID(customerID) {
  const auth = new GoogleAuth({
    scopes: "https://www.googleapis.com/ auth/spreadsheets.readonly",
  });

  auth.getRequestHeaders().then((authorization) => {
    const spreadsheetId = "spreadsheet-id"; //Replace this with the spreadsheet ID .
    const sheetId = 0; // Please set the sheet ID.
    //The Sheet ID is the number that appears after the "gid=" in the spreadsheet URL

    let qs = {
      gid: sheetId,
      tqx: "out:csv",
      tq: `Select A,B,C,D Where B = '${customerID}' and D = 'New' Label A '', B '', C '', D ''`,
    };


    let options = {
      url: `https://docs.google.com/spreadsheets/d/${spreadsheetId}/gviz/tq`,
      qs: qs,
      method: "get",
      headers: authorization,
    };

    request(options, (err, res, result) => {
      if (err) {
        console.log("Error retrieving draft Order ID :((( ", err);
        return;
      }
      let data = result.replaceAll('"', '').split(",");
      console.log("Retrieved from ORDERS tab:", data);
    });
  });
}

Reference:

  • filter()

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật