将按钮单击时的单元格值更改为基于条件的列值

时间:2019-09-15 05:45:32

标签: google-apps-script google-sheets google-sheets-macros

我想通过单击按钮(D3)来更改单元格G2中的值。循环通过的(A列)值应符合B列中的“是”标准。

s

2 个答案:

答案 0 :(得分:1)

尝试一下:

$ grep -B1 -x "[[:digit:]]\{4,6\}" a.txt
>def
123456
--
>ghi
1234

先运行此

它将为您提供一个带有按钮的对话框,用于运行上述脚本。然后,您将在第2列中的“是”之间循环浏览。

function makingChange() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Sheet1');
  var rg=sh.getRange(2,1,sh.getLastRow()-1,2);
  var vA=rg.getValues();
  var rg2=sh.getRange('G2');
  var vg2=rg2.getValue();
  var rgi=sh.getRange('H2');
  var vi=rgi.getValue();
  var sObj={nA:[]};
  var startIdx=!vi?0:vi;
  if(startIdx>=(vA.length-1))startIdx=0;
  for(var i=startIdx;i<vA.length;i++) {
    if(!vg2 && vA[i][1].toString().toLowerCase()=='yes'){
      rg2.setValue(vA[i][0]);
      sh.getRange(i+2,1).activate();
      sh.getRange('H2').setValue(i);
      break
    }
    if(vg2 && vA[i][1].toString().toLowerCase()=='yes' && vA[i][0]!=vg2) {
      rg2.setValue(vA[i][0]);
      sh.getRange(i+2,1).activate();
      rgi.setValue(i);
      break;
    }
    if(vg2 && vA[i][1].toString().toLowerCase()=='no' && i==vA.length-1) {
      rg2.setValue('');
      rgi.setValue('');
      makingChange();//Thats right this runs the script over again
    }
  }
}

电子表格:

enter image description here

答案 1 :(得分:1)

OP的目标是遍历名称列表,该列表以“ yes”的值停在每个名称上。

显然,这需要跳过值为“ no”的名称。在处理列表中的姓氏且“选择”值为“ no”时,存在一个特殊的挑战。在这种情况下,循环应在列表顶部“重新启动”。

以下代码解决了这些意外情况。

使用以下示例数据;该代码应在Paul,Dave,Brett和Ted上停止。
 如果单元格G2中的值为“ Ted”,则下一个选定的名称应为Paul。


示例数据屏幕截图 Sample data screenshot


@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        //if messages contains data payload (map of custom key values)
         if(remoteMessage.getData().size() > 0){
            //handle the data message here           
            sendNotification(remoteMessage);
        }

        //if notification payload
        if (remoteMessage.getNotification() != null){          
            sendNotification(remoteMessage);
        }
    }


private void sendNotification(RemoteMessage remoteMessage){
         int notification_id = (int) System.currentTimeMillis();
         NotificationManager notificationManager = null;
         NotificationCompat.Builder mBuilder;

         String title = remoteMessage.getData().get("title");
         String body = remoteMessage.getData().get("body");
         String type = remoteMessage.getData().get("type");

         //Set pending intent to builder
         Intent intent = new Intent(getApplicationContext(), MainActivity.class);
         PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);

         //Notification builder
         if (notificationManager == null){
             notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
         }


        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = notificationManager.getNotificationChannel(CHANNEL_ID);
            if (mChannel == null){
                mChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance);
                mChannel.setDescription(CHANNEL_DESCRIPTION);
                mChannel.enableVibration(true);
                mChannel.setLightColor(Color.GREEN);
                mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                notificationManager.createNotificationChannel(mChannel);
            }

            mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
            mBuilder.setContentTitle(title)
                    .setSmallIcon(R.drawable.ic_small)
                    .setContentText(body) //show icon on status bar
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                    .setDefaults(Notification.DEFAULT_ALL);
        }else {
            mBuilder = new NotificationCompat.Builder(this);
            mBuilder.setContentTitle(title)
                    .setSmallIcon(R.drawable.ic_small)
                    .setContentText(body)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                    .setDefaults(Notification.DEFAULT_VIBRATE);
        }

        notificationManager.notify(1002, mBuilder.build());
    }