单击后每次都会显示按钮更改文本 [英] Button change text display every after click

查看:73
本文介绍了单击后每次都会显示按钮更改文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Google脚本新手.你能帮我这个忙吗?我想在googlesheet中创建一个按钮,每次单击后都会显示一系列文本.(我不知道该在脚本中添加什么内容)

I am new in Google script. Could you please help me on this. I want to create a button in googlesheet with series of text display every after click. ( I dont know what to put in script)

打开(绿色背景)->单击->CLOSE(红色背景)->单击->OPEN(绿色背景)...等等

OPEN (green background) --> CLICK--> CLOSE ( red background) --> CLICK--> OPEN (green background)...and so on

提前谢谢

推荐答案

我相信您的目标如下.

  • 您想要实现在Google Spreadsheet中单击按钮后将其更改的按钮.
    • 例如,当具有绿色和打开"的按钮被按下时,显示为关闭".单击文本,您想要将其更改为带有红色和关闭"按钮的按钮.文字.

    很遗憾,在当前阶段,Google Apps脚本无法通过编辑文本和颜色直接在Google Spreadsheet上管理图形.因此,需要考虑实现目标的解决方法.在这个答案中,我想提出以下3个变通方法作为方法.

    In the current stage, unfortunately, Google Apps Script cannot directly manage the drawing on Google Spreadsheet by editing the text and color. So it is required to think of the workaround for achieving your goal. In this answer, I would like to propose the following 3 workarounds as the methodology.

    在此替代方法中,图形用作按钮.当将该图形用作按钮时,准备了2张图形,并且两个图形都被包装.每个按钮分别具有 button1 button2 的功能.单击按钮后,将替换z索引.由此,可以实现切换按钮.因为在当前阶段,该图像无法通过Google Apps脚本直接进行编辑.

    In this workaround, the drawing is used as the button. When the drawing is used as the button, 2 drawings are prepared and both drawings are overwrapped. Each button has the function of button1 and button2, respectively. When a button is clicked, the z index is replaced. By this, the switching button can be achieved. Because in the current stage, the image cannot be directly edited by the Google Apps Script.

    1. 要使用示例脚本,请像上面的演示图像一样,将2个按钮创建为图纸.

    1. In order to use the sample script, create 2 buttons as the drawings like the above demo image.

    • 在这种情况下,请包裹两个图纸.
    • 请为每个按钮分配 button1 button2 的功能.

    将以下脚本复制并粘贴到电子表格的容器绑定脚本中并保存.

    Copy and paste the following script to the container-bound script of Spreadsheet and save it.

     const button1 = () => switching("button1");
     const button2 = () => switching("button2");
    
     function switching(name) {
       const ss = SpreadsheetApp.getActiveSpreadsheet();
       const sheet = ss.getActiveSheet();
       const drawings = sheet.getDrawings();
       drawings.forEach(d => d.setZIndex(d.getOnAction() == name ? 0 : 1));
       const temp = ss.insertSheet();
       SpreadsheetApp.flush();
       ss.deleteSheet(temp);
       sheet.activate();
     }
    

  • 单击按钮.这样,脚本将运行并且按钮将被切换.

  • Click the button. By this, the script is run and the button is switched.

    注意:

    • 在这种情况下,为了刷新图形的z索引的更改,需要将焦点更改为其他图纸.这样,单击该按钮时,该按钮会立即闪烁.关于这一点,我希望将来会进行更新.
    • 在此模式中,图形用作按钮.在这种情况下,还准备了两张图,并且在单元格"C3"上放置了一个按钮.另一个是在同一张纸上的另一个位置(作为测试,将按钮置于"E10".).每个按钮分别具有 button1 button2 的功能.单击一个按钮后,被单击的按钮将替换为另一个位置的按钮.这样,可以实现切换按钮.

      In this pattern, the drawing is used as the button. In this case, 2 drawings are also prepared, and one button is put to the cell "C3" and another one is other position (as the test, the button is put to "E10".) in the same sheet. Each button has the function of button1 and button2, respectively. When a button is clicked, the clicked button is replaced with the button of the another position. By this, the switching button can be achieved.

      1. 要使用示例脚本,请像上面的演示图像一样,将2个按钮创建为图纸.

      1. In order to use the sample script, create 2 buttons as the drawings like the above demo image.

      • 在这种情况下,请在单元格"C3"上放置一个按钮.另一个在另一个工作表中放置到另一个位置(作为测试,将按钮置于"E10".).
      • 请为每个按钮分配 button1 button2 的功能.

      将以下脚本复制并粘贴到电子表格的容器绑定脚本中并保存.

      Copy and paste the following script to the container-bound script of Spreadsheet and save it.

       const button1 = () => switching("button1");
       const button2 = () => switching("button2");
      
       function switching(name) {
         const ss = SpreadsheetApp.getActiveSpreadsheet();
         const sheet = ss.getActiveSheet();
         const drawings = sheet.getDrawings();
         if (drawings[0].getOnAction() == name) {
           drawings[1].setPosition(3, 3, 0, 0);
           drawings[0].setPosition(10, 5, 0, 0);
         } else {
           drawings[0].setPosition(3, 3, 0, 0);
           drawings[1].setPosition(10, 5, 0, 0);
         }
       }
      

    • 单击按钮.这样,脚本将运行并且按钮将被切换.

    • Click the button. By this, the script is run and the button is switched.

      注意

      • 在这种情况下,不需要通过聚焦到其他工作表进行刷新.但是,需要将另一个按钮放在同一张纸上.
      • 在此替代方法中,该单元格用作按钮.当单元被用作按钮时,在这种情况下,单元"C3"被设置为按钮.用来.然后OnSelectionChange事件触发器用于执行脚本.

        In this workaround, the cell is used as the button. When the cell is used as the button, in this case, the cell "C3" is used. And the OnSelectionChange event trigger is used for executing the script.

        1. 为了使用示例脚本,请在文本框中输入"OPEN"文本.单元格"C3"的颜色为红色就像上面的演示图像一样.

        1. In order to use the sample script, put the text of "OPEN" and the red color to the cell "C3" like above demo image.

        将以下脚本复制并粘贴到电子表格的容器绑定脚本中并保存.

        Copy and paste the following script to the container-bound script of Spreadsheet and save it.

         function onSelectionChange(e) {
           const range = e.range;
           const sheet = range.getSheet();
           const a1Notation = range.getA1Notation();
           if (a1Notation == "C3") {
             if (range.getValue() == "OPEN") {
               range.setBackground("#ff0000");
               range.setValue("CLOSE");
             } else {
               range.setBackground("#38761d");
               range.setValue("OPEN");
             }
             sheet.getRange("A1").activate();
           }
         }
        

      • 单击单元格"C3".这样,便可以运行脚本并切换单元格的文本和颜色.

      • Click the cell "C3". By this, the script is run and the text and color of the cell are switched.

        注意

        • 在这种情况下,为了再次选择按钮,需要更改选择的单元格.这是当前规范.
          • 这些是简单的示例脚本.因此,请根据您的实际情况对其进行修改.

          这篇关于单击后每次都会显示按钮更改文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆