Copy link to clipboard
Copied
I can break link to cell/table style in a current document, but if I want to do it in a selection, it seem error, anyone would help me to fix it?
Script A (For Document is ok)
BreakLinkToCellStyleDocument();
function BreakLinkToCellStyleDocument(){
var myDoc = app.activeDocument;
myCells = myDoc.stories.everyItem().tables.everyItem().cells.everyItem().getElements();
for (var i=0; i<myCells.length; i++){
myCell = myCells;
myCell.appliedCellStyle = myDoc.cellStyles.item(0), false;
}
}
Script B (For Selection occur error)
BreakLinkToCellStyleSelection();
function BreakLinkToCellStyleSelection(){
var myDoc = app.selection[0];
myCells = myDoc.tables.everyItem().cells.everyItem().getElements();
for (var i=0; i<myCells.length; i++){
myCell = myCells;
myCell.appliedCellStyle = myDoc.cellStyles.item(0), false;
}
}
Copy link to clipboard
Copied
Hi,
app.selection[0] can differ and does not promise a "table" property.
Your function (script B) should split ways, i.e.:
BreakLinkToCellStyleSelection ();
function BreakLinkToCellStyleSelection () {
var
mNone = app.activeDocument.cellStyles.item(0),
mSel = app.selection[0];
switch(mSel.constructor.name) {
case "Cell":
mSel.appliedCellStyle = mNone; break;
case "Table":
mSel.cells.everyItem().appliedCellStyle = mNone;
mSel.cells.everyItem().clearCellStyleOverrides();
break;
default :
if (mSel.hasOwnProperty("tables") && mSel.tables.length)
mSel.tables.everyItem().cells.everyItem().appliedCellStyle = mNone;
break;
}
}
Jarek
Copy link to clipboard
Copied
I just tested your script, it only can select whole table to do, if I select some cells, there still hv cell style
從三星手機發送
Copy link to clipboard
Copied
Hi,
it keep style or keep format only?
if format ==> add a line
mySel.clearCellStyleOverrides();
to "Cell" case
Jarek
Copy link to clipboard
Copied
@creativejoan0425 – is a table style involved that includes a cell style?
Uwe
Copy link to clipboard
Copied
Maybe we should offer increasing levels of cleaning, from the minimalist clearCellStyleOverrides(false) to a complete removing of style attributes:
var breakLinkToCellStyleSelection = function F(/*0|1|2|3*/LEVEL)
// -------------------------------------
// LEVEL 0 => only clear overrides relative to *defined* style attributes
// LEVEL 1 => clear all overrides (through root style)
// LEVEL 2 => clear all overrides and apply [None]
// LEVEL 3 => apply [None] *in depth* (i.e. release all attributes)
{
// Cache
// ---
F.RE_DOC_SPEC || (F.RE_DOC_SPEC=/\/document\[[^\]]+\]/);
var o = app.properties.selection && app.selection[0];
// Validation
// ---
if( !o ) return 0;
while( !('cells' in o) )
{
if( 'cells' in o.parent )
{ o=o.parent; break; }
if( ('tables' in o) && o.tables.length )
{ o=o.tables.everyItem(); break; }
return 0;
}
// [None] style
// ---
var NONE_STYLE = resolve((o.toSpecifier().match(F.RE_DOC_SPEC)||['/'])[0]).
cellStyles.itemByName("$ID/[None]");
// Process
// ---
o = o.cells.everyItem();
o.clearCellStyleOverrides(!!LEVEL);
if( 1 < LEVEL ) o.appliedCellStyle = NONE_STYLE;
if( 2 < LEVEL ) o.properties = NONE_STYLE.properties;
};
breakLinkToCellStyleSelection(3); // warning: deep cleaning!
@+
Marc
Copy link to clipboard
Copied
Marc,
How can we set the Level number in that powerful script?
Copy link to clipboard
Copied
@Jean-Claude – see the last line of the code.
There is a number inside the brackets.
This is the level number.
It's the argument to the function F in line one, where Marc points at four possible values in the comments.
In fact there are more values possible, but suggested are 0, 1, 2, 3.
You could also work with floating point numbers. They will fall back to the appropriate level.
0 … 0.9999… => 0
1 … 1.9999… => 1
2 … 2.9999… => 2
3 … => 3
Very clever! Numbers higher than 3 will not throw an error, but fall back to level 3.
The function is modular, new levels higher than 3 or even sublevels (eg. 1.5) could be easily attached as an added if statement in the process-section of the function.
One could rewrite the snippet and substitute all names of the function F with breakLinkToCellStyleSelection and call the function like that (maybe it's clear to you now):
//Example of calling level 2:
breakLinkToCellStyleSelection(2);
function breakLinkToCellStyleSelection(/*0|1|2|3*/LEVEL){
// -------------------------------------
// LEVEL 0 => only clear overrides relative to *defined* style attributes
// LEVEL 1 => clear all overrides (through root style)
// LEVEL 2 => clear all overrides and apply [None]
// LEVEL 3 => apply [None] *in depth* (i.e. release all attributes)
// Cache
// ---
breakLinkToCellStyleSelection.RE_DOC_SPEC || (breakLinkToCellStyleSelection.RE_DOC_SPEC=/\/document\[[^\]]+\]/);
var o = app.properties.selection && app.selection[0];
// Validation
// ---
if( !o ) return 0;
while( !('cells' in o) )
{
if( 'cells' in o.parent )
{ o=o.parent; break; }
if( ('tables' in o) && o.tables.length )
{ o=o.tables.everyItem(); break; }
return 0;
}
// [None] style
// ---
var NONE_STYLE = resolve((o.toSpecifier().match(breakLinkToCellStyleSelection.RE_DOC_SPEC)||['/'])[0]).
cellStyles.itemByName("$ID/[None]");
// Process
// ---
o = o.cells.everyItem();
o.clearCellStyleOverrides(!!LEVEL);
if( 1 < LEVEL ) o.appliedCellStyle = NONE_STYLE;
if( 2 < LEVEL ) o.properties = NONE_STYLE.properties;
};
@Marc – thank you very much for that snippet!
Uwe
Copy link to clipboard
Copied
Thank you Uwe! That was obvious... 😉
Copy link to clipboard
Copied
Hi,
... next door opened...
thx Marc for sharing
Jarek
Copy link to clipboard
Copied
@Jarek – I tested your code and it worked well.
Here a variation I tested:
BreakLinkToCellStyleSelection();
function BreakLinkToCellStyleSelection () {
var
mNone = app.activeDocument.cellStyles.item(0),
mSel = app.selection[0];
if(mSel.constructor.name === "Cell" || mSel.constructor.name === "Table") {
mSel.cells.everyItem().appliedCellStyle = mNone;
mSel.cells.everyItem().clearCellStyleOverrides();
return;
};
if(mSel.hasOwnProperty("baselineShift")){
mSel.tables.everyItem().cells.everyItem().appliedCellStyle = mNone;
mSel.tables.everyItem().cells.everyItem().clearCellStyleOverrides();
return;
};
};
Uwe
Copy link to clipboard
Copied
For cell Selection is work (but can't keep the style setting after break the link), but I also want to break the table style, but error
For both Break link to cell/table style, I want to keep the style setting after break the link
BreakLinkToTableStyleSelection();
BreakLinkToCellStyleSelection();
function BreakLinkToTableStyleSelection () {
var
mNone = app.activeDocument.tableStyles.item(0),
mSel = app.selection[0];
if(mSel.constructor.name === "Cell" || mSel.constructor.name === "Table") {
mSel.tables.everyItem().appliedTableStyle = mNone;
return;
};
if(mSel.hasOwnProperty("baselineShift")){
mSel.tables.everyItem().appliedTableStyle = mNone;
return;
};
};
function BreakLinkToCellStyleSelection () {
var
mNone = app.activeDocument.cellStyles.item(0),
mSel = app.selection[0];
if(mSel.constructor.name === "Cell" || mSel.constructor.name === "Table") {
mSel.cells.everyItem().appliedCellStyle = mNone;
return;
};
if(mSel.hasOwnProperty("baselineShift")){
mSel.tables.everyItem().cells.everyItem().appliedCellStyle = mNone;
return;
};
};