![](/img/trans.png)
[英]How do I change the text color within a cell of a Google Docs table in a script?
[英]How can I change the color of a specific border of a Google Docs table by using Google Apps Script?
我在 Google Docs 文檔中有幾個表格,所有表格都只有一個單元格,並且只有它們的左邊框可見。 我通過將彼此的邊框寬度設置為 0 來(手動)實現了這一點。我的目標是更改左邊框的顏色並保持其他邊框不可見(我不在乎怎么做,所以如果有辦法實現這個只需為它們設置顏色就可以了,但我還沒有找到更改單個邊框顏色的方法,所以我不得不更改它們(並將不需要的寬度設置為 0))。
這是一張圖片來闡明我的意思(第一個表是表現在的樣子,第二個是我想把它改成的樣子)
我嘗試了以下方法:
tables[i].setBorderColor(myNewColor)
不幸的是,此方法(和 tables[i].setAttributes())僅在左邊框寬度為 1px 時有效。 然而,在我的文檔中,左邊框的寬度為 2.25,因此出於某種原因,代碼不會更改邊框顏色,因為它沒有標准寬度。 有沒有辦法在不改變寬度的情況下改變左邊框顏色,同時保持其他邊框幾乎不可見?
我相信你的目標如下。
不幸的是,在當前階段,似乎每個邊框都無法由 Google 文檔服務 (DocumentApp) 管理。 但是,幸運的是,當使用 Google Docs API 時,這個是可以實現的。 在這個答案中,我想提出一個使用谷歌文檔的示例腳本 API。示例腳本如下。
請將以下腳本復制並粘貼到 Google 文檔的腳本編輯器中。 在使用此腳本之前, 請在 Advanced Google services 啟用 Google Docs API 。
function myFunction() {
const doc = DocumentApp.getActiveDocument();
const documentId = doc.getId();
const body = doc.getBody();
const table = body.getTables()[0]; // Here, the 1st table is used.
const index = body.getChildIndex(table);
const tableStart = Docs.Documents.get(documentId).body.content[index + 1].startIndex;
const borders = ["borderTop", "borderBottom", "borderLeft", "borderRight"];
const resource = {
requests: [
{
updateTableCellStyle: {
tableStartLocation: { index: tableStart },
tableCellStyle: borders.reduce((o, e) => (o[e] = { width: { magnitude: 0, unit: "PT" }, dashStyle: "SOLID", color: { color: { rgbColor: { blue: 0 } } } }, o), {}),
fields: borders.join(",")
}
},
{
updateTableCellStyle: {
tableRange: { tableCellLocation: { tableStartLocation: { index: tableStart }, rowIndex: 0, columnIndex: 0 }, rowSpan: table.getNumRows(), columnSpan: 1 },
tableCellStyle: { borderLeft: { dashStyle: "SOLID", width: { magnitude: 2.25, unit: "PT" }, color: { color: { rgbColor: { red: 1 } } } } },
fields: "borderLeft"
}
}
]
};
Docs.Documents.batchUpdate(resource, documentId);
}
運行此腳本時,Google Document 中的第一個表格僅保留左邊框,左邊框的顏色更改為紅色,如下所示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.