通过 Google Apps 脚本中的单元格循环 ID

Loop ID through cells in Google Apps Script

美好的一天!我希望在之前 sheet 中提交表格时重复采购订单 ID。然而,我当前的代码在整个数据范围内重复了这一点。我可以在修改下面的代码或 loop/if 语句时获得帮助吗?提前谢谢你。

function exportPO() {
var myGooglSheet = SpreadsheetApp.getActive();
var shSource = myGooglSheet.getSheetByName("Source");
var shDatabase = myGooglSheet.getSheetByName("Database");
var lastRow = shDatabase.getLastRow()

var ProductValues = shSource.getRange("B5:C14").getValues();
shDatabase.getRange(lastRow+1,2,ProductValues.length,2).setValues(ProductValues);

var PO = shSource.getRange("C1").getValues();
var lines = 0;

var ProductRow = ProductValues.length;
for(var i=0; i<ProductRow; i++)
  if (ProductValues!=''){
  lines= lines+1;
  shDatabase.getRange(lastRow+lines,1).setValues(PO);
  shDatabase.getRange(lastRow+lines,4).setValue(new Date()).setNumberFormat('yyyy-mm- 
  dd h:mm'); //Submitted On

相对于

var ProductValues = shSource.getRange("B5:C14").getValues();

以上行将数组的数组分配给 ProductValues

相对于

ProductValues!=''

上面的表达式总是returnfalse。您可以将其更改为 ProductValues[i].every(value => value != '') 以将一行中的所有值与空字符串进行比较,如果所有值都不是空字符串,这将 return 为真,否则为假,或将原始表达式更改为某些内容像 ProductValues[i][0]!='' 将每一行的值与空字符串进行比较,第一个索引对应于行,第二个索引对应于列。