Usando as respostas de Vidar e Jacob como base, criei a seguinte solução que permitirá que você escreva = fullCellRef (A1), que copiará o valor e o formato de A1.
Um efeito colateral menor é que, se você arrastar e copiar uma célula com esta fórmula, as novas células inicialmente copiarão a formatação da célula original (como é normal), mas depois mudarão para a formatação mencionada após uma pequena pausa.
Folha de amostra aqui .
/**
* Dummy function to be the equivalent of using simple reference,
* but is used to identify which cells to copy format.
* The immediate effect of =fullCellRef(A1) is the same as =A1
*
* @param {string} value The value of the referred cell
* @return {string} The given value
*/
function fullCellRef(value){
return value;
}
/**
* For each cell with the formula eg B2=fullCellRef(A1), the format of
* the referred cell (eg A1) is copied to the calling cell (eg B2)
*/
function copyFormatting() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var offsetRow = range.getRow() - 1;
var offsetCol = range.getColumn() - 1;
var formulas = range.getFormulas();
var formats = {
fontColors: range.getFontColors(),
backgrounds: range.getBackgrounds(),
fonts: range.getFontFamilies(),
fontWeights: range.getFontWeights(),
fontStyles: range.getFontStyles(),
verticalAlignments: range.getVerticalAlignments(),
horizontalAlignments: range.getHorizontalAlignments(),
numberFormats: range.getNumberFormats()
};
var formulaIsUsed = false;
for (var row = 0; row < formulas.length; row ++ ) {
for (var column = 0; column < formulas[row].length; column ++ ) {
var refersTo = findReferenceCells(formulas[row][column]);
if (refersTo){
formulaIsUsed = true;
var refRow = refersTo.row - offsetRow;
var refCol = refersTo.column - offsetCol;
for (var key in formats) {
formats[key][row][column] = formats[key][refRow][refCol];
}
}
}
}
if (formulaIsUsed) {
range.setBackgrounds(formats.backgrounds);
range.setFontColors(formats.fontColors);
range.setFontFamilies(formats.fonts);
range.setFontWeights(formats.fontWeights);
range.setFontStyles(formats.fontStyles);
range.setVerticalAlignments(formats.verticalAlignments);
range.setHorizontalAlignments(formats.horizontalAlignments);
range.setNumberFormats(formats.numberFormats);
}
}
/**
* Returns the 2D array indices to identify the referred cell.
* @param {string} formula The cell formula
* @return {Array.integer} The row and column array indices
*/
function findReferenceCells(formula) {
if (formula === "") {
return false;
}
var refPattern = /^=fullcellref\(([a-z]{1,2})(\d+)\)$/i;
var matches = refPattern.exec(formula.replace(" ", ""));
matches.shift();
if (!matches) {
return false;
}
// convert cell reference to array indices
var column = colToInteger(matches[0]) - 1;
var row = matches[1] - 1;
return {row: row, column: column};
}
/**
* Converts a column name to a column number
* @param {string} columnName eg "A", "BB"
* @return {integer} Between 1 and 256
*/
function colToInteger(columnName){
var nameParts = columnName.toLowerCase().split();
//97 is char code of "a", but we need 1 based indices
var colNum = nameParts.pop().charCodeAt(0) - 96;
if (nameParts.length === 1){
colNum += 26 * (nameParts.pop().charCodeAt(0) - 96);
}
return colNum;
}