Tillämpa skuggning på alternerande rader och kolumner med villkorlig formatering

Contents
[ ]

Denna artikel använder Excels inbyggda funktioner såsom RAD, KOLUMN och MOD. Här är några detaljer om dessa funktioner för en bättre förståelse av kodsnutten som följer.

  • RAD() funktionen returnerar radnumret för en cellreferens. Om referensparametern utelämnas, antar den att referensen är celladressen där RAD()-funktionen har använts.
  • KOLON() funktionen returnerar kolumnnumret för en cellreferens. Om referensparametern utelämnas, antar den att referensen är celladressen där KOLON()-funktionen har använts.
  • MOD()-funktionen returnerar resten efter att ett nummer har delats av en divisor, där det första parametern till funktionen är det numeriska värdet vars rest du vill hitta och det andra parametern är det tal som används för att dela in i nummerparametern. Om divisorn är 0 kommer den att returnera felen #DIV/0!.

Låt oss börja skriva lite kod för att uppnå detta mål med hjälp av API:et Aspose.Cells for Node.js via C++.

const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create an instance of Workbook or load existing
const book = new AsposeCells.Workbook();
// Access the Worksheet on which desired rule has to be applied
const sheet = book.getWorksheets().get(0);
// Add FormatConditions to the instance of Worksheet
let idx = sheet.getConditionalFormattings().add();
// Access the newly added FormatConditions via its index
const conditionCollection = sheet.getConditionalFormattings().get(idx);
// Define a CellsArea on which conditional formatting will be applicable
// The code creates a CellArea ranging from A1 to I20
const area = AsposeCells.CellArea.createCellArea("A1", "I20");
// Add area to the instance of FormatConditions
conditionCollection.addArea(area);
// Add a condition to the instance of FormatConditions
// For this case, the condition type is expression, which is based on some formula
idx = conditionCollection.addCondition(AsposeCells.FormatConditionType.Expression);
// Access the newly added FormatCondition via its index
const formatCondition = conditionCollection.get(idx);
// Set the formula for the FormatCondition
formatCondition.setFormula1("=MOD(ROW(),2)=0");
// Set the background color and pattern for the FormatCondition's Style
formatCondition.getStyle().setBackgroundColor(AsposeCells.Color.Blue);
formatCondition.getStyle().setPattern(AsposeCells.BackgroundType.Solid);
// Save the result on disk
book.save(path.join(dataDir, "output_out.xlsx"));

Följande ögonblicksbild visar det resulterande kalkylarket som är laddat i Excel-applikationen.

todo:image_alt_text

För att applicera nyanser på alternativa kolumner, behöver du bara ändra formeln =MOD(RAD(),2)=0 till =MOD(KOLUMN(),2)=0, det vill säga; istället för att få radindexet, modifiera formeln för att hämta kolumnindexet.
Det resulterande kalkylarket kommer i detta fall att se ut som följer.

todo:image_alt_text