如何在Google表格中将单元格值从一张工作表中获取(并设置)?

时间:2018-03-05 17:56:36

标签: google-apps-script google-sheets

希望从我的Google表格文件的第一张(POTemplate)中的多个单元格中获取值作为订单输入表单。第22行是表格中的第一行,用于从我们的用户收集有关订单请求的项目的数据  我想要设置这些值的标签(POHistory)将作为每个订单键入POTemplate选项卡的所有订单详细信息的运行日志。记入PO模板日志的每个条目都应包含每个订单'唯一的PO编号(在POTemplate选项卡的单元格N3中找到)& PO日期(POTemplate选项卡的单元格N4)。我真诚地感谢你的帮助。

function submit() {
  var app = SpreadsheetApp;
  var tplSheet = app.getActiveSpreadsheet().getSheetByName("POTemplate");
  var tplFRow = 22, tplLRow = tplSheet.getLastRow();
  var tplRowsNum = tplLRow - tplFRow + 1;
  var tplFCol = 1, tplLCol = 16;
  var tplColsNum = tplLCol - tplFCol + 1;
  var rangeData = tplSheet.getRange(22, 1, tplRowsNum, tplColsNum).getValues();
  var colIndexes = [0, 3, 10, 12, 15];
  var fData = filterByIndexes(rangeData, colIndexes);
  var target = "POHistory";
  var targetSheet = app.getActiveSpreadsheet().getSheetByName(target);
  var tgtRow = targetSheet.getLastRow() + 1;
  var tgtRowsNum = fData.length - tgtRow + 1;
  var tgtCol = 1;
  var tgtColsNum = fData[0].length - 1 + 1;
   targetSheet.getRange(tgtRow, tgtCol, tgtRowsNum, 
tgtColsNum).setValues(fData);
}

function filterByIndexes(twoDArr, indexArr) {
  var fData = [];
  twoDArr = twoDArr.transpose();
  for(var i=0; i<indexArr.length; i++) {
     fData.push(twoDArr[indexArr[i]]);
   }
  return fData.transpose();
}

Array.prototype.transpose = function() {
  var a = this,
      w = a.length ? a.length : 0,
      h = a[0] instanceof Array ? a[0].length : 0;
   if (h === 0 || w === 0) {return [];}
   var i, j, t = [];
  for (i = 0; i < h; i++) {
     t[i] = [];
     for (j = 0; j < w; j++) {
      t[i][j] = a[j][i];
     }
   }
  return t;
}

1 个答案:

答案 0 :(得分:0)

我可以为你提供你所提交的submit()函数的修改。

    function process(){
  submit();
  submitDate();
}

function submitDate() {
  var app = SpreadsheetApp;
  var tplSheet = app.getActiveSpreadsheet().getSheetByName("POTemplate");
  var tplFRow = 1, tplLRow = 21;
  var tplRowsNum = tplLRow - tplFRow + 1;
  var tplFCol = 1, tplLCol = 16;
  var tplColsNum = tplLCol - tplFCol + 1;
  var rangeData = tplSheet.getRange(tplFRow, tplFCol, tplLRow, tplColsNum).getValues();
  var colIndexes = [13];
  var fData = filterByIndexes(rangeData, colIndexes);
  var target = "POHistory";
  var targetSheet = app.getActiveSpreadsheet().getSheetByName(target);
  var tgtRow = targetSheet.getLastRow() + 1;
  var tgtRowsNum = fData.length - tgtRow + 1;
  var tgtCol = 1;
  var tgtColsNum = fData[0].length - 1 + 1;
  Logger.log(fData)
  fData=fData.transpose();
  Logger.log(fData)
  targetSheet.getRange(tgtRow-1, 5, fData.length, 
                       fData[0].length).setValues(fData);
}

function submit() {
  var app = SpreadsheetApp;
  var tplSheet = app.getActiveSpreadsheet().getSheetByName("POTemplate");
  var tplFRow = 22, tplLRow = tplSheet.getLastRow();
  var tplRowsNum = tplLRow - tplFRow + 1;
  var tplFCol = 1, tplLCol = 16;
  var tplColsNum = tplLCol - tplFCol + 1;
  var rangeData = tplSheet.getRange(22, 1, tplRowsNum, tplColsNum).getValues();
  var colIndexes = [0, 3, 10, 12, 15];
  var fData = filterByIndexes(rangeData, colIndexes);
  var target = "POHistory";
  var targetSheet = app.getActiveSpreadsheet().getSheetByName(target);
  var tgtRow = targetSheet.getLastRow() + 1;
  var tgtRowsNum = fData.length - tgtRow + 1;
  var tgtCol = 1;
  var tgtColsNum = fData[0].length - 1 + 1;
  targetSheet.getRange(tgtRow, tgtCol, tgtRowsNum, 
                       tgtColsNum).setValues(fData);
}

function filterByIndexes(twoDArr, indexArr) {
  var fData = [];
  twoDArr = twoDArr.transpose();
  for(var i=0; i<indexArr.length; i++) {
    fData.push(twoDArr[indexArr[i]]);
  }
  return fData.transpose();
}

Array.prototype.transpose = function() {
  var a = this,
      w = a.length ? a.length : 0,
      h = a[0] instanceof Array ? a[0].length : 0;
  if (h === 0 || w === 0) {return [];}
  var i, j, t = [];
  for (i = 0; i < h; i++) {
    t[i] = [];
    for (j = 0; j < w; j++) {
      t[i][j] = a[j][i];
    }
  }
  return t;
}

基本上,类似的代码运行两次。我这样做可能会受到批评,但也许这样对你来说很方便。