SpreadJS 15
Features / Print / Print Area
In This Topic
    Print Area
    In This Topic

    SpreadJS defines a print range in a worksheet by the custom name “Print_Area” whenever the user sets a print area. The main benefits of the “Print_Area” custom name are:

    Interaction with Name Manager

    The SpreadJS Name Manager adds a "Print_Area" custom name with the same reference as the set print area. It can be selected in the worksheet by accessing it through the name box dropdown.

    The users can also manually create or edit the "Print_Area" custom name like any other custom name through the Name Manager dialog. The custom name is neither culture-related nor case-sensitive.

    Note: The “Print_Area” custom name is valid when it is in sheet scope and refers to the range of the sheet. It acts only as a custom name if invalid.

    Hence, users can add a custom name "Print_Area" with the workbook scope, but not affect the print area.

    Interaction with Print Information

    The print range fetched from the “Print_Area” custom name sets the values of the following PrintInfo class methods: rowStart, rowEnd, columnStart, and columnEnd.

    Conversely, the class methods set up the “Print_Area” custom name in the SpreadJS worksheet. It is recommended to set the print range either through the custom name "Print_Area" or through the PrintInfo methods, and not together.

    Examples

    The following code sample shows how the “Print_Area” custom name affects the PrintInfo methods.

    JavaScript
    Copy Code
    // Configure Workbook and Worksheet
    var spread = new GC.Spread.Sheets.Workbook("ss");
    var sheet = spread.getActiveSheet();
    
    var printInfo = sheet.printInfo();
    
    // Print_Area affecting PrintInfo
    sheet.addCustomName("Print_Area", "=A1:B5")
    console.log(printInfo.rowStart())   // Output: 0
    console.log(printInfo.rowEnd())     // Output: 4
    
    sheet.removeCustomName("Print_Area", "")
    console.log(printInfo.rowStart())   // Output: -1
    console.log(printInfo.rowEnd())     // Output: -1
    

    The following code sample shows how the PrintInfo methods affect the “Print_Area” custom name.

    JavaScript
    Copy Code
    // PrintInfo affecting Print_Area
    printInfo.rowStart(1)
    // Assume the rowCount of the sheet is 20
    sheet.getCustomName("Print_Area")
    // Return a NameInfo instance, and the formula is "=2:20"
    console.log(printInfo.rowStart())   // Output: 1
    console.log(printInfo.rowEnd())     // Output: -1
    
    printInfo.rowEnd(15)
    sheet.getCustomName("Print_Area")
    // Return a NameInfo instance, and the formula is "=2:16"
    console.log(printInfo.rowEnd())     // Output: 15
    

    The following code sample shows how the “Print_Area” custom name and the PrintInfo methods affect each other.

    JavaScript
    Copy Code
    // Print_Area and PrintInfo affecting each other
    sheet.addCustomName("Print_Area", "=A1:B5")
    console.log(printInfo.rowStart())   // Output: 0
    console.log(printInfo.rowEnd())     // Output: 4
    
    printInfo.rowStart(1);
    sheet.getCustomName("Print_Area")
    // Return a NameInfo instance, and the formula is "=A2:B5"
    

    Interaction with Formulas

    SpreadJS provides the ability to use the custom name “Print_Area” in formulas. This helps to apply conditional statements that can create dynamic print areas in a worksheet.

    For example, you can enter the formula “=IFERROR(ROWS(Print_Area),"none")” to display the number of rows to print, and display "none" if it does not exist.

    You can also update the print area automatically depending on whether values are inserted in a cell as shown in the code example below.

    JavaScript
    Copy Code
    // Configure Workbook and Worksheet
    var spread = new GC.Spread.Sheets.Workbook("ss");
    var sheet = spread.getActiveSheet();
    
    var printInfo = sheet.printInfo();
    
    // Formula with conditional statement
    sheet.addCustomName("Print_Area", "=IF(Sheet1!$A$1,Sheet1!$B$1:$C$5,Sheet1!$D$5:$F$8)")
    // Assume A1 is a truthy value
    console.log(printInfo.rowStart())
    // Output: 0
    console.log(printInfo.rowEnd())
    // Output: 4
    
    printInfo.rowStart(1);
    sheet.getCustomName("Print_Area")
    // Return a NameInfo instance, and the formula is "=B2:C5"