Search code examples
google-apps-scriptgoogle-sheets

Group rows in uneven sizes in google sheets and create google slides via appscript


I have this Google sheet table and I want to group them into uneven sizes such 2,3,2,3,4,2,2,1,9 and then create Google slides. Below is just an example of the rows.

Group Uneven Sizes

I have this code that groups rows into 3.

function newPresentation() {
  let templateId = "XXX";
  let presentationTemplate = DriveApp.getFileById(templateId);
  let copy = presentationTemplate.makeCopy().getId();
  let deck = SlidesApp.openById(copy);
  let slides = deck.getSlides();
  let dataRange = SpreadsheetApp.getActive().getDataRange();

  // I modified the below script.
  const ar = ["{{audit#}}", "{{results#}}"];
  const [, ...sheetContents] = dataRange.getDisplayValues();
  const values = [...Array(Math.ceil(sheetContents.length / 3))].map((_) => sheetContents.splice(0, 3)); // ref: https://github.com/tanaikech/UtlApp#splitarray
  slides.shift();
  slides.forEach((slide, i) =>
    values[i].forEach((g, k) =>
      ar.forEach((f, j) => slide.replaceAllText(f.replace(/#/, k + 1), g[j]))
    )
  );
}

The above code is working fine, however, I need to group uneven rows. How can I achieve this?


Solution

  • In your showing script, how about the following modification?

    From:

    const values = [...Array(Math.ceil(sheetContents.length / 3))].map((_) => sheetContents.splice(0, 3)); // ref: https://github.com/tanaikech/UtlApp#splitarray
    

    To:

    const values = [2, 3, 2, 3, 4, 2, 2, 1, 9].map(e => sheetContents.splice(0, e));
    

    Note:

    • In the case of the chunk sizes of 2,3,2,3,4,2,2,1,9, the rows are required to be the 1st to 29th rows. But, in your showing image, only the 21 rows exist. In that case, I guessed that 2,3,2,3,4,2,2,1,9 might be required to be 2,3,2,3,4,2,2,1,2. Please be careful about this.