我一直在寻找几天来完成这项工作,但没有成功。我有多个用户使用不同的电子表格,这些电子表格看起来相同,但具有不同的ID和安全性。如果我要更改基本工作表的格式,则需要从那里拉出新的格式,并分发给所有用户工作表,以便使我在各个方面保持一致。
*不想复制值 *不想将工作表复制到“复制的工作表”,然后删除并重命名工作表
答案 0 :(得分:2)
带有“ copyFormatToRange”的示例:
var config = {
// range to modify
range: "A1:C3",
// list of spreadsheets/sheets to modify
spreadSheets: [
{
// from spreadsheets url https://docs.google.com/spreadsheets/d/XXX/edit#gid=YYY
spreadSheetId: 'XXX',
sheetId: YYY // always "0" at the creation of the first (spread)sheet
}
]
};
// update spreadsheets
function updateSpreadSheets() {
for (var i = 0, j = config.spreadSheets.length; i < j; i++) {
// get the spreadsheet to modify
var spreadsheet = SpreadsheetApp.openById(config.spreadSheets[i].spreadSheetId);
// get the sheet to modify
var sheet = spreadsheet.getSheets().filter(function(s) { return s.getSheetId() === config.spreadSheets[i].sheetId; })[0];
// copy from the original sheet into the spreadsheet to modify
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().copyTo(spreadsheet);
// get the new tmp sheet
var sheetTmp = spreadsheet.getSheets()[spreadsheet.getNumSheets()-1];
// get the range from the tmp sheet
var sheetTmpRange = sheetTmp.getRange(config.range);
// copy the format from the tmp sheet to the sheet to modify
sheetTmpRange.copyFormatToRange(sheet, 1, 1, 1, 1);
// remove the tmp sheet
spreadsheet.deleteSheet(sheetTmp);
}
}
此外,如果您需要以下功能来检索ID:
// get spreadsheet id
function getSpreadSheetId() {
Logger.log(SpreadsheetApp.getActiveSpreadsheet().getId());
}
// get spreadsheet sheet id
function getSheetId() {
Logger.log(SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getSheetId());
}
答案 1 :(得分:1)
您可以使用Sheets API从一个电子表格中获取格式并更新另一个电子表格。
Spreadsheets.get
获取源格式updateCells
batchUpdate请求以源格式更新另一个电子表格function copyFormatFromSpreadsheetToSpreadsheet() {
var sourceSsId = SpreadsheetApp.getActiveSpreadsheet().getId(),
destSsId = '##destSpreadsheetIdHere##',
sourceRng = 'Sheet1!A1:B4',
destRng = {
//Must be of same dimensions as sourceRng
//Represents Sheet1!B1:C4
sheetId: 123456789, //use 9 digit GridId
startRowIndex: 0,
endRowIndex: 4, //end exclusive
startColumnIndex: 1,
endColumnIndex: 3,
};
/*Get Format of Source Spreadsheet*/
var rowData = Sheets.Spreadsheets.get(sourceSsId, {
ranges: sourceRng,
fields: 'sheets.data.rowData.values.userEnteredFormat',
}).sheets[0].data[0].rowData;
/*Copy Format to Destination Spreadsheet*/
var request = {
requests: [
{
updateCells: {
rows: rowData,
fields: 'userEnteredFormat',
range: destRng,
},
},
],
};
Sheets.Spreadsheets.batchUpdate(request, destSsId);
}
答案 2 :(得分:0)
使用范围对象的getBackgrounds()
和setBackgrounds()
来首先复制单元格背景,然后在任何电子表格中将其设置为必要的范围。
同样,使用getFontColors()
和setFontColors()
来更改字体颜色。您将在此页面上找到这些功能和其他类似功能。
https://developers.google.com/apps-script/reference/spreadsheet/range#setfontsizesize