【问题标题】:Auto addition of text within cells of column A if one enters a number in the same cell of the column如果在列的同一单元格中输入数字,则在 A 列的单元格中自动添加文本
【发布时间】:2017-04-18 23:09:22
【问题描述】:

我是 Google 脚本的新手,但对电子表格公式和函数有一点了解。

我需要,如果我的用户在 A 列的单元格中输入任何数字(例如,A1 中的 123),它会自动在同一个单元格(作为下拉列表)中给出两个选项 - LAPL/ 2K17/123 和 LA/2K17/123 - 可供选择。

我认为通过电子表格功能无法实现,甚至可以通过 Google Script 实现,因为输入数据的同一单元格必须具有更改的文本?

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    这是一个选项 - 不完全是您正在寻找的,但它可能对您有用。

    function onEdit(e){
    var ss = SpreadsheetApp.getActiveSheet();
    var resp = ss.getDataRange().getValues();
    for(var n=0;n<resp.length;n++)
    { 
      for(var p=0;p<resp[n].length;p++)
      {
        if(resp[n][p].toString().match(/^123/)) { 
          
      var ui = SpreadsheetApp.getUi(); // Same variations.
    
      var result = ui.alert(
         'Please confirm',
         'LAPL/2K17/123?',
          ui.ButtonSet.YES_NO);
    
      // Process the user's response.
      if (result == ui.Button.YES) {
        // User clicked "Yes".
        ui.alert('Setting Value to LAPL/2K17/123');
        ss.getRange(n+1,p+1).setValue('LAPL/2K17/123') 
      } else {
        // User clicked "No" or X in the title bar.
        ss.getRange(n+1,p+1).setValue('LA/2K17/123') 
        ui.alert('Setting Value to LA/2K17/123');
      }
          
          
        };
      }
    }};

    【讨论】:

    • 是的,这是一个很好的解决方法,甚至更好。谢谢!不过只有一个问题——我给出的 123 只是作为一个例子……它可以是任何数字,但脚本当前仅输出 123。它应该根据输入动态更改数字。这意味着,如果在单元格 A10 中输入的数字是 892,则脚本应提供 LAPL/2K17/892 和 LA/2K17/892 选项。抱歉,我在上面的帖子中不够清楚。
    • 我认为如果上述答案是您最初问题的答案,您可以将其标记为已回答并使用您提供的关于第二部分的代码创建一个新帖子。
    猜你喜欢
    • 2016-01-17
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多