注意:这是how to run a menu item script across selected sheet tabs
的后续内容
我试图仅在指定的图纸上运行此脚本,以按照代码中的设置绘制边框。有人可以协助更正代码。我在这篇帖子Stack overflow reference上找到了很多信息,我现在遇到第11行的问题,TypeError:无法调用null的方法“ getRange”。如果我能够合并脚本的边框部分,则不确定如何进行。 test sheet link
function setborders() {
var menu = [{name: "Apply Borders", functionName: "borders"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu("Set Borders", menu);
}
function borders() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
for (i = 0; i < 6; i++) {
var sheet = ss.getSheetByName(sheets[i]);
var range = sheet.getRange("A2:V1000");
var values = range.getValues();
var offsetRow = range.getRowIndex();
range.setBorder(false, false, false, false, false, false);
SpreadsheetApp.flush();
for (var i = 0; i < values.length; i++) {
if (values[i][0]) { // Only for non-empty A column cells
sheet.getRange("A" + (i + offsetRow) + ":V" + (i + offsetRow)).setBorder(true, true, true, true, false, false, "black",SpreadsheetApp.BorderStyle.SOLID);
}
}
for (i = 0; i < values.length; i++) {
if (values[i][0] && values[i][14]) {
sheet.getRange("O" + (i + offsetRow) + ":O" + (i + offsetRow)).setBorder(true, true, true, true, true, true, "black",SpreadsheetApp.BorderStyle.SOLID_THICK);
}
}
for (i = 0; i < values.length; i++) {
if (values[i][0] && values[i][18]) {
sheet.getRange("S" + (i + offsetRow) + ":S" + (i + offsetRow)).setBorder(true, true, true, true, true, true, "black",SpreadsheetApp.BorderStyle.SOLID_THICK);
}
}
for (i = 0; i < values.length; i++) {
if (values[i][0] && values[i][19]) {
sheet.getRange("T" + (i + offsetRow) + ":T" + (i + offsetRow)).setBorder(true, true, true, true, true, true, "black",SpreadsheetApp.BorderStyle.SOLID_THICK);
}
}
for (i = 0; i < values.length; i++) {
if (values[i][0] && values[i][20]) {
sheet.getRange("U" + (i + offsetRow) + ":U" + (i + offsetRow)).setBorder(true, true, true, true, true, true, "black",SpreadsheetApp.BorderStyle.SOLID_THICK);
}
}
for (i = 0; i < values.length; i++) {
if (values[i][0] && values[i][21]) {
sheet.getRange("V" + (i + offsetRow) + ":V" + (i + offsetRow)).setBorder(true, true, true, true, true, true, "black",SpreadsheetApp.BorderStyle.SOLID_THICK);
}
}
}
}
答案 0 :(得分:1)
不确定是否有更好的方法可以做到这一点,但这是一个解决方案:
type Team = {
name: string;
city: string
}
type Position = {
position: number;
}
const Component = () => {
const [list, setList] = useState<Array<Team | Position >>(teams)
list.map((item: Team | Position) => {
return 'name' in item ? (
<div>
<p>{item.name}</p>
<p>{item.city}</p>
</div>
) : (
<p>{item.position}</p>
)
})
}
答案 1 :(得分:0)
该代码具有一些缺陷,这些缺陷使您难以将答案应用于先前的问题
for (i = 0; i < values.length; i++)
建议
清理代码
移动所有
if (values[i][0] && values[i][/*a number */]) {
// Call to setBorder
}
合而为一
for (i = 0; i < values.length; i++) {...}
删除for(var i = 0; i < allsheets.length; i++)
if(!['Current Sales','Pending Orders','Subcontract Orders','Partial Shipped','Quotes','Archived Sales'].indexOf(sheets.getName())) return;
下方添加var sheet=allsheets[s]
答案 2 :(得分:0)
function setborders() {
var menu = [{name: "Apply Borders", functionName: "borders"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu("Set Borders", menu);
}
function borders() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet_name = ['Current Sales','Pending Orders', 'Subcontract Orders', 'Partial Shipped', 'Quotes', 'Archived Sales'];
for (s=0; s<sheet_name.length; s++) {
var sheet = ss.getSheetByName(sheet_name[s]);
var range = sheet.getRange("A2:V1000");
var values = range.getValues();
var offsetRow = range.getRowIndex();
range.setBorder(false, false, false, false, false, false);
SpreadsheetApp.flush();
for (var i = 0; i < values.length; i++) {
if (values[i][0]) { // Only for non-empty A column cells
sheet.getRange("A" + (i + offsetRow) + ":V" + (i + offsetRow)).setBorder(true, true, true, true, false, false, "black",SpreadsheetApp.BorderStyle.SOLID);
}
}
for (i = 0; i < values.length; i++) {
if (values[i][0] && values[i][14]) {
sheet.getRange("O" + (i + offsetRow) + ":O" + (i + offsetRow)).setBorder(true, true, true, true, true, true, "black",SpreadsheetApp.BorderStyle.SOLID_THICK);
}
}
for (i = 0; i < values.length; i++) {
if (values[i][0] && values[i][18]) {
sheet.getRange("S" + (i + offsetRow) + ":S" + (i + offsetRow)).setBorder(true, true, true, true, true, true, "black",SpreadsheetApp.BorderStyle.SOLID_THICK);
}
}
for (i = 0; i < values.length; i++) {
if (values[i][0] && values[i][19]) {
sheet.getRange("T" + (i + offsetRow) + ":T" + (i + offsetRow)).setBorder(true, true, true, true, true, true, "black",SpreadsheetApp.BorderStyle.SOLID_THICK);
}
}
for (i = 0; i < values.length; i++) {
if (values[i][0] && values[i][20]) {
sheet.getRange("U" + (i + offsetRow) + ":U" + (i + offsetRow)).setBorder(true, true, true, true, true, true, "black",SpreadsheetApp.BorderStyle.SOLID_THICK);
}
}
for (i = 0; i < values.length; i++) {
if (values[i][0] && values[i][21]) {
sheet.getRange("V" + (i + offsetRow) + ":V" + (i + offsetRow)).setBorder(true, true, true, true, true, true, "black",SpreadsheetApp.BorderStyle.SOLID_THICK);
}
}
}
}
这解决了!