Search code examples
apigoogle-apps-scriptgoogle-sheetsgoogle-sheets-api

ContentService.createTextOutput removes array but array needed array can be like "[]" or [] in Google Apps Script


Google Apps Script removes array My function is

function getAllProducts() {
  const spreadsheetId = '000000-PA2yI4OmUjTn3xz9o4faP-8-iPOniBYc0kuKc';
  const rangeName = 'Products!A2:I1000';
  try {
    const values = Sheets.Spreadsheets.Values.get(spreadsheetId, rangeName).values;
    if (!values) {
      return ContentService.createTextOutput("No Product Found");
    }
    return ContentService.createTextOutput(values);
  } catch (err) {
  }
}

it returns ,,,,,,,,https,,,,,,,,,hngui but needed [[, , , , , , , , https], [, , , , , , , , hngui]]


Solution

  • function getAllProducts() {
      const spreadsheetId = '000000-PA2yI4OmUjTn3xz9o4faP-8-iPOniBYc0kuKc';
      const rangeName = 'Products!A2:I1000';
      try {
        const values = Sheets.Spreadsheets.Values.get(spreadsheetId, rangeName).values;
        if (!values) {
          return ContentService.createTextOutput("No Product Found");
        }
        Logger.log(values);
        return ContentService.createTextOutput(JSON.stringify(values));
      } catch (err) {
      }
    }