From f86f6a4de996da1410667e55c8043fb782c3562e Mon Sep 17 00:00:00 2001 From: Nidhi Date: Mon, 26 Apr 2021 12:11:18 +0530 Subject: [PATCH] Only consider headings till value grid size, but consider all values till this size (#4115) * Only consider headings till value grid size, but consider all values till this size * Switched from null to blank for consistency * Whoops, we don't do null row objects anymore --- .../com/external/config/GetValuesMethod.java | 28 ++++++------------- .../java/com/external/domains/RowObject.java | 6 +++- .../external/config/GetValuesMethodTest.java | 2 +- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/GetValuesMethod.java b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/GetValuesMethod.java index 992ec87379..2c3f2874b7 100644 --- a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/GetValuesMethod.java +++ b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/GetValuesMethod.java @@ -3,7 +3,6 @@ package com.external.config; import com.appsmith.external.exceptions.pluginExceptions.AppsmithPluginError; import com.appsmith.external.exceptions.pluginExceptions.AppsmithPluginException; import com.external.domains.RowObject; -import com.external.utils.SheetsUtil; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -176,17 +175,20 @@ public class GetValuesMethod implements Method { return this.objectMapper.createArrayNode(); } - final String headerRange = valueRanges.get(0).get("range").asText(); + int valueSize = 0; + for (int i = 0; i < values.size(); i++) { + valueSize = Math.max(valueSize, values.get(i).size()); + } + final String valueRange = valueRanges.get(1).get("range").asText(); headers = (ArrayNode) headers.get(0); - Set columnsSet = sanitizeHeaders(headers, headerRange, valueRange); + Set columnsSet = sanitizeHeaders(headers, valueSize); final List> collectedCells = new LinkedList<>(); - final String[] headerArray = columnsSet.toArray(new String[columnsSet.size()]); - final String range = valueRanges.get(1).get("range").asText(); + final String[] headerArray = columnsSet.toArray(new String[0]); - final Matcher matcher = findOffsetRowPattern.matcher(range); + final Matcher matcher = findOffsetRowPattern.matcher(valueRange); matcher.find(); final int rowOffset = Integer.parseInt(matcher.group(1)); final int tableHeaderIndex = Integer.parseInt(methodConfig.getTableHeaderIndex()); @@ -202,21 +204,9 @@ public class GetValuesMethod implements Method { return this.objectMapper.valueToTree(collectedCells); } - private Set sanitizeHeaders(ArrayNode headers, String headerRange, String valueRange) { + private Set sanitizeHeaders(ArrayNode headers, int valueSize) { final Set headerSet = new LinkedHashSet<>(); int headerSize = headers.size(); - - final Matcher matcher1 = sheetRangePattern.matcher(headerRange); - matcher1.find(); - final int headerStart = SheetsUtil.getColumnNumber(matcher1.group(1)); - final int headerEnd = SheetsUtil.getColumnNumber(matcher1.group(2)); - - final Matcher matcher2 = sheetRangePattern.matcher(valueRange); - matcher2.find(); - final int valuesStart = SheetsUtil.getColumnNumber(matcher2.group(1)); - final int valuesEnd = SheetsUtil.getColumnNumber(matcher2.group(2)); - - final int valueSize = (valuesEnd - valuesStart + 1); final int size = Math.max(headerSize, valueSize); // Manipulation to find valid headers for all columns diff --git a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/domains/RowObject.java b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/domains/RowObject.java index a805fb6241..db17321f15 100644 --- a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/domains/RowObject.java +++ b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/domains/RowObject.java @@ -48,9 +48,13 @@ public class RowObject { public RowObject(String[] headerValues, String[] rowValues, int rowIndex) { this.valueMap = new LinkedHashMap<>(rowValues.length + 1); - for (int i = 0; i < rowValues.length; i++) { + int i = 0; + for (; i < rowValues.length; i++) { valueMap.put(headerValues[i], rowValues[i]); } + while (i < headerValues.length) { + valueMap.put(headerValues[i++], ""); + } this.currentRowIndex = rowIndex; this.rowIndex = String.valueOf(rowIndex); diff --git a/app/server/appsmith-plugins/googleSheetsPlugin/src/test/java/com/external/config/GetValuesMethodTest.java b/app/server/appsmith-plugins/googleSheetsPlugin/src/test/java/com/external/config/GetValuesMethodTest.java index ceb3eb531a..ef7708abba 100644 --- a/app/server/appsmith-plugins/googleSheetsPlugin/src/test/java/com/external/config/GetValuesMethodTest.java +++ b/app/server/appsmith-plugins/googleSheetsPlugin/src/test/java/com/external/config/GetValuesMethodTest.java @@ -105,7 +105,7 @@ public class GetValuesMethodTest { Assert.assertNotNull(result); Assert.assertTrue(result.isArray() && result.size() == 8); - Assert.assertNull(result.get(0).get("Some")); + Assert.assertTrue("".equalsIgnoreCase(result.get(0).get("Some").asText())); Assert.assertTrue("Some".equalsIgnoreCase(result.get(3).get("Some").asText())); }