选择付款时将发票数据从一个 sheet 复制到另一个

Issue copying invoice data from one sheet to another when payment is selected

我希望在选择付款方式后将发票中选择的产品保存到同一工作簿中的另一个 sheet。这里是 a copy 的 sheet。 sheet 的工作原理:

1) 用户将 "x" 放入 "Protocol Selection" 中的选择列(工作中)

2) 在工作簿 "Patient Invoice" 的下一个 sheet 中,生成了一张包含瓶数的发票(工作中)

3) I want the past invoices (with product, date, pill-count, etc.) to be copied over to the "Past Invoices" sheet when the "Method of Payment" is selected .这是一个下拉数据验证单元格。 (不工作)

有没有不用自定义脚本的方法?如果不是,脚本是什么?

可能不是最有效的方法,但确实有效...

脚本

function makePastInvoice() {
  var patientInvoice = SpreadsheetApp.getActive().getSheetByName('Patient Invoice');
  var pastInvoices = SpreadsheetApp.getActive().getSheetByName('Past Invoices');

  // vistor details
  var visitDate = patientInvoice.getRange("D3").getValue();
  var patientName = patientInvoice.getRange("G5").getValue();
  var doctorName = patientInvoice.getRange("I5").getValue();
  var programLength = patientInvoice.getRange("I3").getValue();
  var invoiceNumber = patientInvoice.getRange("G3").getValue();
  var total = patientInvoice.getRange("K30").getValue();
  var paymentMethod = patientInvoice.getRange("D5").getValue();
  var visitorDetails = [visitDate, patientName, doctorName, programLength, invoiceNumber, total, paymentMethod];

  var lastRow = pastInvoices.getLastRow();
  var currentRow = lastRow + 1;

  // set values of patient details to Past Invoices sheet
  for (var i = 0; i < visitorDetails.length; i++) {
    pastInvoices.getRange(currentRow, i+1).setValue(visitorDetails[i]);
  }

  // 15 possible max items (per Past Invoices)
  var productIDs = patientInvoice.getRange("A8:A22").getValues();

  // count number of products
  var productCount = 0;
  for(var i = 0; i < productIDs.length; i++) {
    if (productIDs[i] > "") {
      productCount++;
    }
  }

  // Patient Invoice
  var firstItemRow = 8;

  // Past Invoices
  var firstItemCol = 8 // column H

  // loop through purchases
  for(var i = 0; i < productCount; i++) { 

    // purchase details
    var productName = patientInvoice.getRange(firstItemRow, 2).getValue(); // col B 
    var dosage = patientInvoice.getRange(firstItemRow, 5).getValue(); // col E
    var numBottles = patientInvoice.getRange(firstItemRow, 6).getValue(); // col F 
    var purchaseDetails = [productName, dosage, numBottles];

    // set values of purchase details to Past Invoices sheet
    for(var j = 0; j < purchaseDetails.length; j++) {
      pastInvoices.getRange(currentRow, firstItemCol).setValue(purchaseDetails[j])
      firstItemCol ++;
    }
    firstItemRow++;
  }
}

触发器

我确定有一种方法可以在付款方式更改或选择任何其他方法时触发脚本,但我创建了一个带有 drawing 的 "submit" 触发器并将其分配给 makePastInvoice函数。

此外,您可能希望将 "Visit Date" 列的格式更改为 date