[SOLVED] Protection of a specific column lazarus / 49-FPSpreadsheet ---------------------------------------------------------------------- rca · 2025-08-02 15:19 Is it possible to protect the contents of a specific column without having to do it cell by cell? For example, I have 4 columns with this data: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   sWorksheetGrid1.Cells[1,1] := 'Column1';  sWorksheetGrid1.Cells[1,2] := 200;  sWorksheetGrid1.Cells[1,3] := 124;  sWorksheetGrid1.Cells[1,4] := 682;  sWorksheetGrid1.Cells[1,5] := 456;   sWorksheetGrid1.Cells[2,1] := 'Column2';  sWorksheetGrid1.Cells[2,2] := 450;  sWorksheetGrid1.Cells[2,3] := 218;  sWorksheetGrid1.Cells[2,4] := 346;  sWorksheetGrid1.Cells[2,5] := 260;   sWorksheetGrid1.Cells[3,1] := 'Column3';  sWorksheetGrid1.Cells[3,2] := 120;  sWorksheetGrid1.Cells[3,3] := 368;  sWorksheetGrid1.Cells[3,4] := 874;  sWorksheetGrid1.Cells[3,5] := 240;   sWorksheetGrid1.Cells[4,1] := 'Column4';  sWorksheetGrid1.Cells[4,2] := 620;  sWorksheetGrid1.Cells[4,3] := 485;  sWorksheetGrid1.Cells[4,4] := 128;  sWorksheetGrid1.Cells[4,5] := 346;  If I only want to protect the contents of Column 1 and 3, cell by cell, I do: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  cellprot: TsCellProtections;  cell: PCell;  i: integer;begin  //Unlock Column 2  for i:=0 to 4 do  begin    cell := sWorksheetGrid1.Worksheet.FindCell(i,1);    cellprot := sWorksheetGrid1.Worksheet.ReadCellProtection(cell);    sWorksheetGrid1.Worksheet.WriteCellProtection(cell, cellprot - [cpLockCell]);  end;   //Unlock Column 4  for i:=0 to 4 do  begin    cell := sWorksheetGrid1.Worksheet.FindCell(i,3);    cellprot := sWorksheetGrid1.Worksheet.ReadCellProtection(cell);    sWorksheetGrid1.Worksheet.WriteCellProtection(cell, cellprot - [cpLockCell]);  end;   sWorksheetGrid1.Worksheet.Protect(true);end; However, I have some real cases where the number of rows is almost 1000... I tried doing it with "WriteColFormatIndex": Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  fmt: TsCellFormat;  idx: Integer; begin  InitFormatRecord(fmt);  fmt.Protection:=[cpLockCell];  fmt.UsedformattingFields := fmt.UsedFormattingFields + [uffProtection];   idx := sWorksheetGrid1.Workbook.AddCellFormat(fmt);   //Lock Column 1  sWorksheetGrid1.Worksheet.WriteColFormatIndex(1, idx);   //Lock Column 3  sWorksheetGrid1.Worksheet.WriteColFormatIndex(3, idx);end  I appreciate any help you can give me. ---------------------------------------------------------------------- wp · 2025-08-02 16:14 Doing the protection by column is basically correct. But you must know that you must apply the column format before adding cells: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // Creating a worksheet in which data can be entered only in column 1 (except for its header cell)program project1;uses  fpSpreadsheet, fpsTypes, fpsUtils, xlsxOOXML;var  b: TsWorkbook;  sh: TsWorksheet;  r: Cardinal;  header: PCell;  fmt: TsCellFormat;  idx: Integer;begin  b := TsWorkbook.Create;  sh := b.AddWorksheet('Test');   // Protect the worksheet (note: this affects only the file, you can still write to the sheet by code)  sh.Protect(true);   // Define the cell format for an unlocked cell  InitFormatRecord(fmt);  fmt.Protection := [];  // remove protection flags  fmt.UsedFormattingFields := fmt.UsedFormattingFields + [uffProtection];   // Apply the cell format to the entire column to be unlocked  // Important: this must happen before any data are entered.  idx := b.AddCellFormat(fmt);  sh.WriteColFormatIndex(1, idx);   // Add data to the cells  header := sh.WriteText(0, 1, 'Data');  for r := 1 to 100 do    sh.WriteNumber(r, 1, Random);   // The column header ("Data") should be protected  fmt.Protection := [cpLockCell];   // Add cell-locking flag  sh.WriteCellFormat(header, fmt);   // Save  b.WriteToFile('test.xlsx', true);   // Clean up  b.Free;end. ---------------------------------------------------------------------- rca · 2025-08-02 17:12 @wp Thanks for your explanation and help. It works like a charm.