Skip to content

Commit

Permalink
Merge pull request #19 from L21s/feature/rows_and_columns_on_object
Browse files Browse the repository at this point in the history
Feature/rows and columns on object
  • Loading branch information
kerilz authored Jun 12, 2024
2 parents 89483ea + a4ccf72 commit d0e59da
Showing 1 changed file with 114 additions and 27 deletions.
141 changes: 114 additions & 27 deletions src/taskpane/taskpane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@

import { base64Images } from "../../base64Image";
import * as M from "../../lib/materialize/js/materialize.min";
import Shape = PowerPoint.Shape;

const rowLineName = "RowLine";
const columnLineName = "ColumnLine";
const SLIDE_WIDTH = 960;
const SLIDE_HEIGHT = 540;
const SLIDE_MARGIN = 8;
const CONTENT_MARGIN = { top: 126, bottom: 60, right: 54, left: 58 };
const CONTENT_HEIGHT = SLIDE_HEIGHT - CONTENT_MARGIN.top - CONTENT_MARGIN.bottom;
const CONTENT_WIDTH = SLIDE_WIDTH - CONTENT_MARGIN.right - CONTENT_MARGIN.left;

Office.onReady((info) => {
if (info.host === Office.HostType.PowerPoint) {
Expand Down Expand Up @@ -95,47 +102,127 @@ function insertImageByBase64(base64Name: string) {
}

export async function createRows(numberOfRows: number) {
const lineDistance = 354 / numberOfRows;
let top = 126;
await runPowerPoint(async (powerPointContext) => {
const singleSelectedShapeOrNull = await getSingleSelectedShapeOrNull(powerPointContext);
if (singleSelectedShapeOrNull) {
await createRowsForObject(numberOfRows, singleSelectedShapeOrNull, powerPointContext);
} else {
await createRowsForSlide(numberOfRows, powerPointContext);
}
});
}

export async function createColumns(numberOfRows: number) {
await runPowerPoint(async (powerPointContext) => {
const singleSelectedShapeOrNull = await getSingleSelectedShapeOrNull(powerPointContext);
if (singleSelectedShapeOrNull) {
await createColumnsForObject(numberOfRows, singleSelectedShapeOrNull, powerPointContext);
} else {
await createColumnsForSlide(numberOfRows, powerPointContext);
}
});
}

export async function createRowsForSlide(numberOfRows: number, powerPointContext: PowerPoint.RequestContext) {
const shapes = powerPointContext.presentation.getSelectedSlides().getItemAt(0).shapes;

const lineDistance = CONTENT_HEIGHT / numberOfRows;
let top = CONTENT_MARGIN.top;

await renderRows(shapes, numberOfRows, top, lineDistance, SLIDE_WIDTH - SLIDE_MARGIN * 2, SLIDE_MARGIN)
await powerPointContext.sync();
}

export async function createRowsForObject(numberOfColumns: number, selectedShape: Shape, powerPointContext: PowerPoint.RequestContext) {
await powerPointContext.sync();
const shapes = powerPointContext.presentation.getSelectedSlides().getItemAt(0).shapes;


const lineDistance = selectedShape.height / numberOfColumns;
const selectedShapeRight = selectedShape.left + selectedShape.width;
const lineWidth = SLIDE_WIDTH - CONTENT_MARGIN.right - selectedShapeRight;

let top = selectedShape.top;

await renderRows(shapes, numberOfColumns, top, lineDistance, lineWidth, selectedShapeRight)
await powerPointContext.sync();
}

export async function renderRows(
shapes: PowerPoint.ShapeCollection,
numberOfRows: number, initialTop: number,
lineDistance: number, lineWidth: number, left: number) {
let top = initialTop;
for (let _i = 0; _i <= numberOfRows; _i++) {
await runPowerPoint((powerPointContext) => {
const shapes = powerPointContext.presentation.getSelectedSlides().getItemAt(0).shapes;
const line = shapes.addLine(PowerPoint.ConnectorType.straight);
line.name = rowLineName;
line.left = 8;
line.top = top;
line.height = 0;
line.width = 944;
line.lineFormat.color = "#000000";
line.lineFormat.weight = 0.5;
});
const line = shapes.addLine(
PowerPoint.ConnectorType.straight,
{ height: 0.5, left: left, top: top, width: lineWidth }
);
line.name = rowLineName;
line.lineFormat.color = "#000000";

top += lineDistance;
}
}

export async function createColumns(numberOfColumns: number) {
const lineDistance = 848 / numberOfColumns;
let left = 58;
export async function createColumnsForSlide(numberOfColumns: number, powerPointContext: PowerPoint.RequestContext) {
const shapes = powerPointContext.presentation.getSelectedSlides().getItemAt(0).shapes;

const lineDistance = CONTENT_WIDTH / numberOfColumns;

let left = CONTENT_MARGIN.left;

await renderColumns(shapes, numberOfColumns, left, lineDistance, SLIDE_HEIGHT - SLIDE_MARGIN * 2, SLIDE_MARGIN)
await powerPointContext.sync();
}

export async function createColumnsForObject(numberOfColumns: number, selectedShape: Shape, powerPointContext: PowerPoint.RequestContext) {
await powerPointContext.sync();
const shapes = powerPointContext.presentation.getSelectedSlides().getItemAt(0).shapes;

const lineDistance = selectedShape.width / numberOfColumns;
const selectedShapeBottom = selectedShape.top + selectedShape.height;
const lineHeight = SLIDE_HEIGHT - CONTENT_MARGIN.bottom - selectedShapeBottom;

let left = selectedShape.left;

await renderColumns(shapes, numberOfColumns, left, lineDistance, lineHeight, selectedShapeBottom)
await powerPointContext.sync();
}

export async function renderColumns(
shapes: PowerPoint.ShapeCollection,
numberOfColumns: number, initialLeft: number,
lineDistance: number, lineHeight: number, top: number) {
let left = initialLeft;
for (let _i = 0; _i <= numberOfColumns; _i++) {
await runPowerPoint((powerPointContext) => {
const shapes = powerPointContext.presentation.getSelectedSlides().getItemAt(0).shapes;
const line = shapes.addLine(PowerPoint.ConnectorType.straight);
line.name = columnLineName;
line.left = left;
line.top = 8;
line.height = 524;
line.width = 0;
line.lineFormat.color = "#000000";
line.lineFormat.weight = 0.5;
});
const line = shapes.addLine(
PowerPoint.ConnectorType.straight,
{ height: lineHeight, left: left, top: top, width: 0.5 }
);
line.name = columnLineName;
line.lineFormat.color = "#000000";

left += lineDistance;
}
}


export async function getSingleSelectedShapeOrNull(context: PowerPoint.RequestContext) {
let selectedShapes = context.presentation.getSelectedShapes();
let clientResult = selectedShapes.getCount();
await context.sync();
let selectedShapesCount = clientResult.value;
if (selectedShapesCount != 1) {
console.log("error 154");
// TODO error handling
return null;
}

let selectedShape = selectedShapes.getItemAt(0);
return selectedShape.load();
}

export async function insertSticker(color) {
await runPowerPoint((powerPointContext) => {
const today = new Date();
Expand Down

0 comments on commit d0e59da

Please sign in to comment.