r/GoogleAppsScript • • 4d ago

Question PrintScreen of the WebApp for Reports

Hey Guys

I have a Web App and I like a create a report of screenshot this WebApp.

But WebApp are in the Google Workplace, and report have needs parameters before report that a filter inside the WebApp.

Can you give me a north or that impossible?

Sorry my bad english xD

0 Upvotes

1 comment sorted by

2

u/No-Sheepherder7116 10h ago

Not impossible, but the screenshot can't happen on the Apps Script side. Apps Script runs on Google's servers with no browser and no screen, so there is nothing for it to capture. The capture has to happen in the user's browser, after the filters are applied. That actually fits your case well, because the filters live in the Web App itself.

Option 1: the simple route — print the filtered view to PDF

Add a button to your Web App page that calls window.print(), and add print CSS that hides the filter controls and buttons. The user applies the filters, clicks the button, and saves as PDF. What they see is exactly what goes in the report, and no server code is needed:

<button onclick="window.print()">Save report as PDF</button>
@media print {
  .filters, button {
    display: none;
  }
}

Option 2: if you really need an image file saved to Drive

Capture the report in the browser with the html2canvas library, then send the image to a server function that saves it. Put the report inside an element with an id, for example id="report", and add a save button.

In your HTML file:

document.getElementById('saveReport').addEventListener('click', () => {
  html2canvas(document.getElementById('report')).then(canvas => {
    google.script.run
      .withSuccessHandler(name => alert('Saved as ' + name))
      .withFailureHandler(err => alert('Save failed: ' + err.message))
      .saveScreenshot(canvas.toDataURL('image/png'));
  });
});

In Code.gs:

function saveScreenshot(dataUrl) {
  const base64 = dataUrl.split(',')[1];
  const blob = Utilities.newBlob(
    Utilities.base64Decode(base64),
    'image/png',
    'report-' + new Date().toISOString().slice(0, 10) + '.png'
  );
  const file = DriveApp.createFile(blob);
  return file.getName();
}

Two things to watch for:

  • A time-driven trigger can't do this. There is no logged-in browser in a trigger, so someone has to open the Web App, apply the filters, and click the button. If the report needs to run on a schedule with no user present, generate the report from the same data on the server instead of screenshotting the page.
  • The filters are not a problem for the screenshot itself, because the capture only sees the page as it currently looks. But if you want a shareable report link, pass the filters as parameters in the Web App URL and read them in doGet(e) with e.parameter, so the page loads already filtered before anyone captures it.
  • html2canvas doesn't render everything perfectly — iframes, cross-origin images, and some newer CSS can come out wrong — so keep the report element simple and test it with your actual layout.

If the report is just data from a Sheet, Option 1 is what I'd do. Use Option 2 only when you literally need a picture of the Web App.