Newer
Older
smart-metering-front / src / views / business / lab / excelEdit / methods / pagingSeal.ts
dutingting on 8 Apr 1 KB 暂存
/**
 * 骑缝章
 * 思路:根据sheet页数去裁剪图片
 *  */
export default function bindPagingSeal(
  spread: any,
  url: string, // 图片的源路径或 Base64 编码字符串。
  width: number, // 图片的宽度。
  height: number, // 图片的高度。
  startRow = 0, // 起始行
  x = 0, // 图片左上角的 x 坐标。
  y = 0, // 图片左上角的 y 坐标。
  name?: string, // 图片的名称。
) {
  const sheetCount = spread.getSheetCount()
  const itemPictureWidth = width / sheetCount // 每份章的宽度
  const itemPictureWidthProportion = itemPictureWidth / width // 每个占章总宽度的比例
  console.log('几个sheet页', sheetCount)
  console.log('每份章的宽度', itemPictureWidth)
  console.log('每个占章总宽度的比例', itemPictureWidthProportion)

  for (let i = 0; i < sheetCount; i++) {
    const sheet = spread.getSheet(i)
    // 获取工作表的最后一列索引
    const lastColumnIndex = sheet.getColumnCount() - 1
    const picture = sheet.shapes.addPictureShape(
      name || 'picture',
      url,
      x,
      y,
      itemPictureWidth,
      height,
    )
    // 设置图片位置到最右边
    picture.startRow(startRow) // 设置起始行
    picture.startColumn(lastColumnIndex) // 设置起始列
    picture.startColumnOffset(0 - itemPictureWidth) // 调整偏移量以适应图片宽度
    // 可选:设置结束行列以更好地控制图片范围
    // picture.endRow(0)
    picture.endColumn(lastColumnIndex)
    // 裁剪
    picture.pictureFormat({
      crop: {
        left: i * itemPictureWidthProportion, // The left value% of the image will be cropped
        right: 1 - (i + 1) * itemPictureWidthProportion, // The right value% of the image will be cropped
        top: 0, // The top value% of the image will be cropped
        bottom: 0, // The bottom value% of the image will be cropped
      },
    })
  }
}