【问题标题】:How can Google Apps Script to delete a specific sheet by nameGoogle Apps 脚本如何按名称删除特定工作表
【发布时间】:2020-07-04 07:34:22
【问题描述】:

在电子表格中,我有一个名为“Sheet1”的工作表,它是我创建的所有工作表的摘要。我正在尝试从“Sheet1”中删除特定行,它是按名称对应的工作表。 “x”是脚本中的一个变量,我可以成功地从“Sheet1”中删除第“5”行,但不能从工作表中删除“5”行。

function myFunction () {
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1AGDGqi7hh-vr96EuCCDl0PqdcVtwweoWpB2Lj0ajHR4/edit#gid=0");
var sheet = ss.getSheetByName("Sheet1");  
var x = 5; 
sheet.activate().deleteRow(x);  
var source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(x);
SpreadsheetApp.getActiveSpreadsheet().deleteSheet(source);  

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    您甚至不需要在删除之前激活工作表。

    var source = ss.getSheetByName("Sheet"+x);// get the sheet object
    ss.deleteSheet(source);// done !  
    

    【讨论】:

    • 工作表名称是“5”,而不是“Sheet5”。如果我输入 getSheetByName(x),它不起作用。
    • sheetNames 需要是字符串类型,试试 x.toString()
    • 如何使其成为字符串类型?
    • 就像我在上面写的一样,在这里查看信息:w3schools.com/jsref/jsref_tostring_number.asp
    • 按名称删除特定工作表。我将其设为 [var x = x.toString()]。我也按照您的建议使用 [ var source = ss.getActiveName(x) ; ss.deleteSheet(source) ] 因为我的原始代码不起作用。 [ var source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(x); SpreadsheetApp.getActiveSpreadsheet().deleteSheet(source); ]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多