用JavaScript通过C++管理评论和笔记
Contents
[
Hide
]
介绍
评论用于在单元格中添加额外信息。Aspose.Cells for JavaScript通过C++提供两种添加评论的方法。第一种是在设计器文件中手动创建评论,然后使用Aspose.Cells导入。这些评论然后被引入。第二种是在运行时使用Aspose.Cells API添加评论。本文讨论使用Aspose.Cells API添加评论,还会介绍评论的格式化方法。
添加注释
通过调用Comments集合的CommentCollection.add(number, number)方法(封装在Worksheet对象中)在单元格中添加评论。可以通过传递评论索引从Comments集合中访问新Comment对象。在访问Comment对象后,可以使用note属性自定义评论内容。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Add Comment Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
// Instantiating a Workbook object
const workbook = new Workbook();
// Adding a new worksheet to the Workbook object
const sheetIndex = workbook.worksheets.add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
const worksheet = workbook.worksheets.get(sheetIndex);
// Adding a comment to "F5" cell
const commentIndex = worksheet.comments.add("F5");
// Accessing the newly added comment
const comment = worksheet.comments.get(commentIndex);
// Setting the comment note
comment.note = "Hello Aspose!";
// Saving the Excel file
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'book1.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Comment added successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
注释格式设置
还可以通过配置其高度、宽度和字体设置来格式化注释的外观。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Add Comment</title>
</head>
<body>
<h1>Add Comment Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
// Creating a new Workbook object
const workbook = new Workbook();
// Adding a new worksheet to the Workbook object
const sheetIndex = workbook.worksheets.add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
const worksheet = workbook.worksheets.get(sheetIndex);
// Adding a comment to "F5" cell
const commentIndex = worksheet.comments.add("F5");
// Accessing the newly added comment
const comment = worksheet.comments.get(commentIndex);
// Setting the comment note
comment.note = "Hello Aspose!";
// Setting the font size of a comment to 14
comment.font.size = 14;
// Setting the font of a comment to bold
comment.font.isBold = true;
// Setting the height of the font to 10
comment.heightCM = 10;
// Setting the width of the font to 2
comment.widthCM = 2;
// Saving the Excel file
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'book1.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook created and saved. Click the download link to get the file.</p>';
});
</script>
</html>
向注释添加图像
在Microsoft Excel 2007中,还可以将图像添加为单元格注释的背景。在Excel 2007中,可以通过以下步骤完成这一操作。(假设您已经添加了单元格注释。)
1.右键单元格,然后选择显示/隐藏注释,清除注释中的任何文本。 1.点击注释的边框进行选择。 1.选择格式,然后选择注释。 1.在颜色和线条选项卡上,展开颜色列表。 1.单击填充效果。 1.单击图片选项卡。 1.在图片选项卡上,单击选择图片。 1.定位并选择图片。
- 点击 确定 直到所有对话框都关闭。
Aspose.Cells 也提供了这个功能。下面是一个代码示例,从头开始创建一个 XLSX 文件,并在单元格"A1"中添加一个以图片作为背景的评论。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Add Comment with Image</title>
</head>
<body>
<h1>Add Comment with Image Example</h1>
<p>
Select an existing Excel file to modify (optional). If no file is selected, a new workbook will be created.
</p>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<p>
Select an image file to attach to the comment (required):
</p>
<input type="file" id="imageInput" accept="image/*" />
<br/><br/>
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Worksheet, Cell } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const resultEl = document.getElementById('result');
resultEl.innerHTML = '';
const fileInput = document.getElementById('fileInput');
const imageInput = document.getElementById('imageInput');
if (!imageInput.files.length) {
resultEl.innerHTML = '<p style="color: red;">Please select an image file for the comment.</p>';
return;
}
// Load or create workbook
let workbook;
if (fileInput.files.length) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
workbook = new Workbook(new Uint8Array(arrayBuffer));
} else {
workbook = new Workbook();
}
// Get a reference of comments collection with the first sheet
const comments = workbook.worksheets.get(0).comments;
// Add a comment to cell A1
const commentIndex = comments.add(0, 0);
const comment = comments.get(commentIndex);
// Set the comment note and font name (converted from set/get to properties)
comment.note = "First note.";
comment.font.name = "Times New Roman";
// Load image file data
const imgFile = imageInput.files[0];
const imgArrayBuffer = await imgFile.arrayBuffer();
const imgUint8 = new Uint8Array(imgArrayBuffer);
// Set image data to the shape associated with the comment
comment.commentShape.fill.imageData = imgUint8;
// Save the workbook to browser-downloadable file
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'book1.out.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
resultEl.innerHTML = '<p style="color: green;">Comment added with image successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>