Google Apps脚本中“对话”窗口中的动态文本框

时间:2018-11-08 15:35:34

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

我是应用脚本和JavaScript的新手,所以这可能是一个非常基本的问题,但我似乎在任何地方都找不到该问题的答案。

我希望有一个基于特定字段中单元格值的动态对话框。

到目前为止,这是我的代码。

 

   function showPrompt() {
  var L,sourceSheet,ss,targetSh;

  ss = SpreadsheetApp.getActiveSpreadsheet();
  targetSh = ss.getSheetByName('Stop Time Generator');
  sourceSheet = ss.getSheetByName('My Shift Log');

  var ui = SpreadsheetApp.getUi(); // Same variations.
  var values = targetSh.getRange('I2:I').getValues()

  L = values.length;

  for (var i = 0; i < L; i++){
    if(values[i]!=""){
    var result = ui.prompt(
      'Please Log waste weight for: '  + values[i],
        ui.ButtonSet.OK_CANCEL);

    // Process the user's response.
    var button = result.getSelectedButton();
    var text = result.getResponseText();
    Logger.log('text: ' + text)

    if (button == ui.Button.OK) {
      targetSh.getRange(i+2,10).setValue(text)
      sourceSheet.getRange('d10').copyTo(targetSh.getRange('T1') , {contentsOnly:true});
      if (!text) {
        ui.alert('Please enter a number value or click CANCEL');
      }
    } else if (button == ui.Button.CANCEL) {
      // User clicked "Cancel".
      ui.alert('Your response wasn\'t recorded');
      break;
    } else if (button == ui.Button.CLOSE) {
      // User clicked X in the title bar.
      ui.alert('Your response wasn\'t recorded');
    }
   }
 }
}

我试图弹出对话框,要求首先在单元格I2中要求值的权重,然后在提交后再询问I3中值的权重,等等。

到目前为止,它只是弹出在文本字段上方带有0的循环中

任何帮助将不胜感激。

如果我没有找到类似的问题,请链接到我。

谢谢

1 个答案:

答案 0 :(得分:0)

尝试此代码。

function showPrompt() {
  var L,sourceSheet,ss,targetSh;

  ss = SpreadsheetApp.getActiveSpreadsheet();
  targetSh = ss.getSheetByName('Sheet3');
  sourceSheet = ss.getSheetByName('My Shift Log');

  var ui = SpreadsheetApp.getUi(); // Same variations.
  var values = ss.getSheetByName('Stop Time Generator').getRange('I2:I').getValues()

  L = values.length;

  for (var i = 0; i < L; i++){

    var result = ui.prompt(
      'Please Log waste weight for: '  + values[i],
        ui.ButtonSet.OK_CANCEL);

    // Process the user's response.
    var button = result.getSelectedButton();
    var text = result.getResponseText();
    Logger.log('text: ' + text)

    if (button == ui.Button.OK) {
      targetSh.getRange('Q1').setValue(text)
      sourceSheet.getRange('d10').copyTo(targetSh.getRange('T1') , {contentsOnly:true});
      if (!text) {
        ui.alert('Please enter a value or click CANCEL');
      }
    } else if (button == ui.Button.CANCEL) {
      // User clicked "Cancel".
      ui.alert('Your response wasn\'t recorded');
      break;
    } else if (button == ui.Button.CLOSE) {
      // User clicked X in the title bar.
      ui.alert('Your response wasn\'t recorded');
    }
  }
}