希望这个问题还没有得到回答。我花了相当多的时间搜索,虽然我找到了类似的帖子,但没有一个完全符合我的要求。
我想使用 Google Apps 脚本将单个工作表从 Google 电子表格复制到另一个 Google 电子表格中,并且我想保留格式(包括合并的单元格)。有没有办法做到这一点?我尝试了以下功能:
复制到()复制格式到范围()
但这些方法仅适用于同一个电子表格,不允许在不同电子表格之间复制数据。有没有人有什么建议?谢谢!
希望这个问题还没有得到回答。我花了相当多的时间搜索,虽然我找到了类似的帖子,但没有一个完全符合我的要求。
我想使用 Google Apps 脚本将单个工作表从 Google 电子表格复制到另一个 Google 电子表格中,并且我想保留格式(包括合并的单元格)。有没有办法做到这一点?我尝试了以下功能:
复制到()复制格式到范围()
但这些方法仅适用于同一个电子表格,不允许在不同电子表格之间复制数据。有没有人有什么建议?谢谢!
你看过这里吗:
https://developers.google.com/apps-script/reference/spreadsheet/sheet#copyTo(Spreadsheet)
复制到(电子表格)
将工作表复制到另一个电子表格。目标电子表格可以是源。新电子表格的名称为“[原始电子表格名称]的副本”。
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheet = source.getSheets()[0];
var destination = SpreadsheetApp.openById("ID_GOES HERE");
sheet.copyTo(destination);
如果要复制特定电子表格的工作表,可以使用:
SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet();
这将创建当前工作表的副本并使该副本处于活动状态。
如果有人只想复制特定工作表中特定范围的格式
/**
copying full formatting including sizez and merging from one range to new location
https://stackoverflow.com/questions/25106580/copy-value-and-format-from-a-sheet-to-a-new-google-spreadsheet-document
below first coordinates of original range we want to copy and then cooridinaes of the begining of the place ino which we want to copy our range
@param startColumnOfOriginal {Number}
@param startRowOfOriginal {Number}
@param numberOfRows {Number}
@param numberOfColumns {Number}
@param startColumnOfTarget {Number}
@param startRowOfTarget {Number}
@param sheetOfOrigin {Sheet} sheet object of where our source is
@param sheetOfTarget {Sheet} sheet object where we want to copy it
*/
function copyFullFormatting(startRowOfOriginal,startColumnOfOriginal,numberOfRows
,numberOfColumns, startRowOfTarget, startColumnOfTarget, sheetOfOrigin, sheetOfTarget
){
const sourceRange = sheetOfOrigin.getRange(
startRowOfOriginal, startColumnOfOriginal, numberOfRows, numberOfColumns)
const targetRange = sheetOfTarget.getRange(
startRowOfTarget, startColumnOfTarget, numberOfRows, numberOfColumns)
sourceRange.copyFormatToRange(sheetOfTarget,startColumnOfOriginal, startColumnOfTarget+ numberOfColumns, startRowOfTarget, startRowOfTarget+numberOfRows )
//iterating over rows of source range
for(var rowNumb=startRowOfOriginal;rowNumb<startRowOfOriginal+numberOfRows;rowNumb++ ){
const targetRowNumb = rowNumb-startRowOfOriginal+startRowOfTarget
sheetOfTarget.setRowHeight(targetRowNumb, sheetOfOrigin.getRowHeight(rowNumb))
}
// iterating over columns in target range
for (var colNumb=startColumnOfOriginal;colNumb<startColumnOfOriginal+numberOfColumns;colNumb++ ){
const targetColNumb = colNumb-startColumnOfOriginal+startColumnOfTarget
sheetOfTarget.setColumnWidth(targetColNumb, sheetOfOrigin.getColumnWidth(colNumb))
}
}
试试这个功能。
CopyTable("sda12wrsdsdaada121", "sheetA", "sheetB", "A1:D200")
/**
* Copy data and formatting from source to destination
* @param {string} srcId id of source
* @param {string} srcSheetName sheet's name of source
* @param {string} destSheetName sheet's name of destination
* @param {string} copyRange A1Notation of range
*/
function CopyTable(srcId, srcSheetName, destSheetName, copyRange){
var srcSpreadSheet = SpreadsheetApp.openById(srcId);
var srcSheet = srcSpreadSheet.getSheetByName(srcSheetName);
var destSpreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var destSheet = destSpreadSheet.getSheetByName(destSheetName);
destSheet.clear();
var srcRange = srcSheet.getRange(copyRange);
var values = srcRange.getValues();
var background = srcRange.getBackgrounds();
var fontColor = srcRange.getFontColors();
var fontFamily = srcRange.getFontFamilies();
var fontLine = srcRange.getFontLines();
var fontSize = srcRange.getFontSizes();
var fontStyle = srcRange.getFontStyles();
var fontWeight = srcRange.getFontWeights();
var textStyle = srcRange.getTextStyles();
var horAlign = srcRange.getHorizontalAlignments();
var vertAlign = srcRange.getVerticalAlignments();
var bandings = srcRange.getBandings();
var mergedRanges = srcRange.getMergedRanges();
var destRange = destSheet.getRange(copyRange);
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.setTextStyles(textStyle);
destRange.setHorizontalAlignments(horAlign);
destRange.setVerticalAlignments(vertAlign);
for (let i in bandings){
let srcBandA1 = bandings[i].getRange().getA1Notation();
let destBandRange = destSheet.getRange(srcBandA1);
destBandRange.applyRowBanding()
.setFirstRowColor(bandings[i].getFirstRowColor())
.setSecondRowColor(bandings[i].getSecondRowColor())
.setHeaderRowColor(bandings[i].getHeaderRowColor())
.setFooterRowColor(bandings[i].getFooterRowColor());
}
for (let i = 0; i < mergedRanges.length; i++) {
destSheet.getRange(mergedRanges[i].getA1Notation()).merge();
}
for (let i = 1; i <= srcRange.getHeight(); i++) {
let width = srcSheet.getColumnWidth(i);
destSheet.setColumnWidth(i, width);
}
for (let i = 1; i <= srcRange.getWidth(); i++){
let height = srcSheet.getRowHeight(i);
destSheet.setRowHeight(i, height);
}
}