更新:我写了for循环来解决合并的单元格和列宽问题。只是需要一些帮助边框!
我正在将一系列单元格从一个Google Spreadsheet复制到另一个Google Spreadsheet。困难的部分是我试图保持所有格式。到目前为止,我已经能够保留所有格式,除了3项内容:单元格边框,各种合并的单元格和列宽。任何帮助将不胜感激。抱歉,昨天才发现有关Google App脚本的信息,而我以前从未使用过JS。
function importTable() {
// Source spreadsheet
var srcSpreadSheet = SpreadsheetApp.openById("SpreadSheet-ID");
var scrSheet = srcSpreadSheet.setActiveSheet(srcSpreadSheet.getSheetByName("Sheet1"));
// Destination spreadsheet
var destSpreadSheet = SpreadsheetApp.openById("SpreadSheet-ID");
var destSheet = destSpreadSheet.setActiveSheet(destSpreadSheet.getSheetByName("Test"));
destSheet.clear();
// Get data and formatting from the source sheet
var range = scrSheet.getRange(1, 2, 24, 16);
var values = range.getValues();
var background = range.getBackgrounds();
var banding = range.getBandings();
var mergedRanges = range.getMergedRanges();
var fontColor = range.getFontColors();
var fontFamily = range.getFontFamilies();
var fontLine = range.getFontLines();
var fontSize = range.getFontSizes();
var fontStyle = range.getFontStyles();
var fontWeight = range.getFontWeights();
var horAlign = range.getHorizontalAlignments();
var textStyle = range.getTextStyles();
var vertAlign = range.getVerticalAlignments();
// Put data and formatting in the destination sheet
var destRange = destSheet.getRange(1, 2, 24, 16);
destRange.setValues(values);
destRange.setBackgrounds(background);
destRange.setFontColors(fontColor);
destRange.setFontFamilies(fontFamily);
destRange.setFontLines(fontLine);
destRange.setFontSizes(fontSize);
destRange.setFontStyles(fontStyle);
destRange.setFontWeights(fontWeight);
destRange.setHorizontalAlignments(horAlign);
destRange.setTextStyles(textStyle);
destRange.setVerticalAlignments(vertAlign);
// Iterate through to put merged ranges in place
for (var i = 0; i < mergedRanges.length; i++) {
destSheet.getRange(mergedRanges[i].getA1Notation()).merge();
}
// Iterate through to get the column width of the source destination
for (var i = 1; i < 18; i++) {
var width = scrSheet.getColumnWidth(i);
destSheet.setColumnWidth(i, width);
}
// Iterate through to get the row heighth of the source destination
for (var i = 1; i < 27; i++){
var height = scrSheet.getRowHeight(i);
destSheet.setRowHeight(i, height);
}
}
答案 0 :(得分:0)
该解决方法如何?在此变通办法中,使用“类表”的copyTo()和“类范围”的copyTo()。我认为您的情况有几个答案。因此,请将此视为其中之一。
此脚本的流程如下。
function importTable2() {
// Source
var sourceSheet = SpreadsheetApp.openById("SpreadSheet-ID").getSheetByName("Sheet1");
var sourceRange = sourceSheet.getRange(1, 2, 24, 16);
// Destination
var destSS = SpreadsheetApp.openById("SpreadSheet-ID");
var destSheet = destSS.getSheetByName("Test");
var destRange = destSheet.getRange(1, 2, 24, 16);
destSheet.clear();
// Copy
var copiedsheet = sourceRange.getSheet().copyTo(destSS);
copiedsheet.getRange(sourceRange.getA1Notation()).copyTo(destRange);
copiedsheet.getRange(sourceRange.getA1Notation()).copyTo(destRange, SpreadsheetApp.CopyPasteType.PASTE_COLUMN_WIDTHS, false);
destSS.deleteSheet(copiedsheet);
}
SpreadSheet-ID
。如果我误解了您的问题,请告诉我。我想修改它。