Home > Mobile >  Print multiple data per ID in Google Sheet
Print multiple data per ID in Google Sheet

Time:10-27

In this Sample

CodePudding user response:

In your situation, how about creating a new Google Spreadsheet including each page? When this is reflected in a sample script, it becomes as follows.

Sample script:

function myFunction() {
  const srcSs = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = srcSs.getSheetByName("TEMPLATE");
  const values = sheet.getRange("C2").getDataValidation().getCriteriaValues()[0].getValues().flat().filter(String);
  const dstSs = SpreadsheetApp.create("tempSpreadsheet");
  values.forEach(v => {
    sheet.getRange("C2").setValue(v);
    SpreadsheetApp.flush();
    const tempSheet = sheet.copyTo(srcSs);
    const range = tempSheet.getDataRange();
    range.copyTo(range, {contentsOnly: true});
    tempSheet.getRange("B2:2").clear().clearDataValidations();
    tempSheet.getDrawings().forEach(e => e.remove());
    tempSheet.copyTo(dstSs);
    srcSs.deleteSheet(tempSheet);
  });
  dstSs.deleteSheet(dstSs.getSheets()[0]);
}
  • When this script is run, a new Google Spreadsheet of tempSpreadsheet is created to the root folder. When you see it, each tab has each sheet without using the formulas. By this, you can print out the Spreadsheet by one manual process.

Note:

  • When I saw your sample Spreadsheet, /** @OnlyCurrentDoc */ is used in your script. When you use my proposed script, please remove /** @OnlyCurrentDoc */. By this, the scope of https://www.googleapis.com/auth/spreadsheets is automatically detected and used it. Please be careful about this.
  • This sample script is for your sample Spreadsheet. So when the spreadsheet is changed, this script might not be able to be used. Please be careful about this.

References:

  • Related