如何使用Google脚本删除图表?

时间:2018-11-15 17:03:22

标签: google-apps-script

我编写了一个脚本,可以根据不同选项卡上的数据自动生成图表(单击新工作表上的按钮之后)。

我现在想添加一个“重置”按钮,该按钮将自动删除或删除生成的图表(以便我可以通过另一个按钮生成不同的图表)。

任何人都可以帮助我吗?我已经尝试了以下方法,但无济于事。...

import sqlite3

with sqlite3.connect(":memory:") as conn: # Using a context manager
    c = conn.cursor()

    c.execute("""
              CREATE TABLE IF NOT EXISTS testing(
                  some_code INTEGER,
                  data TEXT)
              """)

    c.executemany("""
                  INSERT INTO testing VALUES (?, ?)
                  """, [[1, 'hi'], [2, 'bye'], [1, 'something']])

    # Query the new database using a parameterized query
    c.execute("select * from testing where some_code = ?", (1,))

    if len(list(c)) > 0: # Exhausts the iterator and then throws the result away 
        print("Printing result set 1")
        for row in c:
            print(row)
        print("End of result set 1")
        print()

    # Repeat the query
    c.execute("select * from testing where some_code = ?", (1,))
    print("Printing result set 2")
    for row in c: # iterate the cursor 
        print(row)
    print("End of result set 2")
    print()

    # And one more time but using fetchall()
    c.execute("select * from testing where some_code = ?", (1,))
    data = c.fetchall() # Exhaust the iterator but assign a list to a name
    print("Printing result set 3")
    for row in data:
        print(row)
    print("End of result set 3")
    print()

    # And we can keep on printing without re-querying
    print("Printing result set 4")
    for row in data: 
        print(row)
    print("End of result set 4")
    print()

2 个答案:

答案 0 :(得分:0)

从工作表中删除所有图表

function delAllChartsFromSheet(name) {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName(name);
  var chts=sh.getCharts();
  for(var i=0;i<chts.length;i++){
    sh.removeChart(chts[i]);
  }
}

Sheet Reference

答案 1 :(得分:0)

代码对我有用,我将其重写为:

function remove_charts_from_sheet(sheet_name) {
  const tab = SpreadsheetApp.getActive().getSheetByName(sheet_name);
  const charts = tab.getCharts();
  for(let i = 0; i < charts.length; i++){
    tab.removeChart(charts[i]);
  }
}