我正在使用 cfspreadsheet 创建电子表格对象。想要将某些单个单元格设为 protected (只读)。请让我知道是否有人以前尝试过这个。
我确实尝试将单元格格式设置为锁定,但似乎不起作用。这是示例代码:
<cfset a = spreadsheetnew()>
<cfset format1 = structNew()>
<cfset format1.locked=true>
<cfset SpreadsheetFormatCell(a,format1,1,1)>
<cfspreadsheet action="write" filename="#expandpath('.')#/test.xls" name="a" overwrite="true">
谢谢。
最佳答案
锁定单元格没有任何作用 unless the sheet is protected即使用 cfspreadsheet 的 password
属性。但这样做有一些负面影响......
保护工作表会锁定所有单元格。这意味着您基本上必须通过应用格式来“解锁”其他所有内容。理论上你可以解锁整个工作表:
<cfset SpreadsheetFormatCellRange (sheet, {locked=false}, 1, 1, maxRow, maxCol)>
但是,这会产生填充工作表中每个单元格的不良影响。因此,如果您将文件读入查询,则查询将包含约 65,536 行和 256 列。即使您只明确填充了几个单元格。
锁定功能更适合您希望锁定除少数单元格之外的所有内容(而不是相反)的情况。除非那是你正在做的事情,考虑到所有的负面影响,我可能不会打扰它。
副作用示例
<cfset testFile = "c:/test.xls">
<cfset sheet = spreadsheetNew()>
<!--- only unlocking 100 rows to demonstrate --->
<cfset SpreadsheetFormatCellRange (sheet, {locked=false}, 1, 1, 100, 10)>
<!--- populate two cells --->
<cfset SpreadsheetSetCellValue(sheet,"LOCKED",1,1)>
<cfset SpreadsheetSetCellValue(sheet,"UNLOCKED",2,1)>
<!--- make one cell locked --->
<cfset SpreadsheetFormatCell(sheet, {locked=true}, 1, 1)>
<cfspreadsheet action="write"
name="sheet"
fileName="#testFile#"
password=""
overwrite="true" >
<!--- now see it is filled with empty cells --->
<cfspreadsheet action="read"
query="sheetData"
src="#testFile#" >
<cfdump var="#sheetData#" label="Lots of empty cells" />
关于coldfusion - 如何创建带有 protected 单元格的 cfspreadsheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12696971/