[英]Google Sheets save the style of sheet while merging it to another sheet
I need to save the styles of the sheets when I combine them into a mainsheet.当我将它们组合成一个主表时,我需要保存这些表的样式。 Here is the code:这是代码:
function myFunction()
{
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var newSheet = activeSpreadsheet.getSheetByName("MainSheet");
if (newSheet != null) {
activeSpreadsheet.deleteSheet(newSheet);
}
newSheet = activeSpreadsheet.insertSheet();
newSheet.setName("MainSheet");
const ss = SpreadsheetApp.getActiveSpreadsheet();
const allSheets = ss.getSheets();
const allSheets_names=allSheets.map(sheet=>sheet.getSheetName())
const dataRange = "A1:M";
const checkRange = "A1:A";
const neededSheets = ["Cats", "Dogs"];
const filteredListOfSheetsNames = [];
neededSheets.forEach(function(ns){
var i = neededSheets.indexOf(ns);
filteredListOfSheetsNames[i]=[];
allSheets_names.forEach( (as,index) => {
if (as.indexOf(ns)>-1){
filteredListOfSheetsNames[i].push(as);
}
}
)
const filteredListOfSheets = filteredListOfSheetsNames[i].map(name =>ss.getSheetByName(name));
var array = [];
filteredListOfSheets.forEach(function(sheet){var values = sheet.getRange(1,1,sheet.getLastRow(),13).getValues(); array.push(values);});
array = [].concat.apply([],array);
if(array.length > 0){
newSheet.getRange(1,i*13+1, array.length, array[0].length).setValues(array);
}
}
)
}
Currently, I get all the information correctly.目前,我正确地获得了所有信息。 On one side of the mainsheet I get sheets that contain a word "dog" in them and on another side of the mainsheet I get the sheets that contain a word "cat".在主表的一侧,我得到了包含“狗”一词的表,而在主表的另一侧,我得到了包含“猫”一词的表。 The issue is that the font, colors disappear after I combine all the sheets.问题是字体、颜色在我组合所有工作表后消失了。 How should I keep the styles when combining the sheets?组合床单时如何保持样式?
Here is an example of the MainSheet on how it should look like, but it should also take all of the styles from the sheets.这是 MainSheet 的一个示例,说明它应该是什么样子,但它也应该采用工作表中的所有样式。
You can use copyTo
to copy the formatting from one sheet to another.您可以使用copyTo
将格式从一张纸复制到另一张纸。 See documentation here .请参阅此处的文档。
Here is a minimal example based off of your script that copies the formatting to mainSheet
from sheets Cats
and Dogs
.这是一个基于脚本的最小示例,该示例将格式从工作表Cats
和Dogs
复制到mainSheet
。 This script assumes that the script will be attached to the spreadsheet on which it is to operate, which you're likely already doing.此脚本假定该脚本将附加到要对其进行操作的电子表格,您可能已经在这样做了。
function myFunction() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const mainSheet = ss.getSheetByName("MainSheet");
const neededSheets = ["Cats", "Dogs"];
mainSheet.clearFormats().clear() // clear formatting and values on main sheet
for(var i=0; i<neededSheets.length; i++)
{
var sheet = ss.getSheetByName(neededSheets[i]) ;
var values = sheet.getRange(1,1,sheet.getLastRow(),13).getValues();
mainSheet.getRange(1,i*13+1, values.length, values[0].length).setValues(values);
var sourceRange = sheet.getRange(1,1,sheet.getLastRow(),13) // define the range that has the formatting you want to copy
var targetRange = mainSheet.getRange(1,i*13+1, values.length, values[0].length) // define the range that you want to copy the formatting to
sourceRange.copyTo(targetRange, {formatOnly:true}) // copy the format
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.