首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Google脚本-同时接收数组中的多个单元格值

Google脚本-同时接收数组中的多个单元格值
EN

Stack Overflow用户
提问于 2022-08-04 11:37:12
回答 2查看 76关注 0票数 0

我已经开发了一个脚本,但是它太慢了(>1分钟)。我读过更少的值调用减少了时间,所以我会问如何做到这一点。其想法是只在一行上取单元格值,然后对于每个"let",取该数组的一部分。

你知道怎么做吗?守则是:

代码语言:javascript
运行
复制
function createPDF() {
  
  const currentSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CRM");

  let profe_nombre = currentSheet.getRange(14,40).getValues();
  let profe_direccion = currentSheet.getRange(16,40).getValues();
  let profe_dni = currentSheet.getRange(15,40).getValues();
  let profe_iban = currentSheet.getRange(17,40).getValues();
  let profe_paypal = currentSheet.getRange(18,40).getValues();
  let profe_email = currentSheet.getRange(19,40).getValues();
  let factura_nombre = currentSheet.getRange(26,39).getValues();
  let factura_fecha = currentSheet.getRange(23,40).getDisplayValues();
  let factura_importe = currentSheet.getRange(22,40).getValues();
  let factura_notas = currentSheet.getRange(26,38).getValues();

  const docFile = DriveApp.getFileById("sheetID");
  const tempFolder = DriveApp.getFolderById("folderID");
  const pdfFolder = DriveApp.getFolderById("folderID");
  const tempFile = docFile.makeCopy(tempFolder);
  const tempDocFile = DocumentApp.openById(tempFile.getId());
  const body = tempDocFile.getBody()
  body.replaceText("{profe_nombre}", profe_nombre);
  body.replaceText("{profe_direccion}", profe_direccion);
  body.replaceText("{profe_dni}", profe_dni);
  body.replaceText("{profe_iban}", profe_iban);
  body.replaceText("{profe_paypal}", profe_paypal);
  body.replaceText("{profe_email}", profe_email);
  body.replaceText("{factura_nombre}", factura_nombre);
  body.replaceText("{factura_fecha}", factura_fecha);
  body.replaceText("{factura_importe}", factura_importe);
  body.replaceText("{factura_notas}", factura_notas);
  tempDocFile.saveAndClose();
  const pdfContentBlob = tempFile.getAs(MimeType.PDF);
  pdfFolder.createFile(pdfContentBlob).setName(factura_nombre + ".pdf");
  tempFolder.removeFile(tempFile);
  SpreadsheetApp.getUi().alert("Factura creada");

}

function apendiceRemesa() {

  const SheetCRM = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CRM");
  const SheetRemesas = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Remesas");
  
  let remesa_cuenta = SheetCRM.getRange(17,40).getValue();
  let remesa_importe = SheetCRM.getRange(22,40).getValue();
  let remesa_nombre = SheetCRM.getRange(14,40).getValue();
  let remesa_concepto = SheetCRM.getRange(26,39).getValue();
  const remesa_estado_pago = SheetCRM.getRange(24,40).getValue();
  let remesa_estado_pago_fila = SheetCRM.getRange(23,43).getValue();
  let remesa_estado_pago_columna = SheetCRM.getRange(24,43).getValue();

  if (remesa_estado_pago == 'P') {
    SheetRemesas.appendRow([remesa_cuenta,remesa_importe,remesa_nombre,remesa_concepto]);
    SpreadsheetApp.getActiveSheet().getRange(remesa_estado_pago_fila, remesa_estado_pago_columna).setValue('RM');
    
  } else {
  SpreadsheetApp.getUi().alert('ERROR. Esta factura ya fue pagada')
  }
  

}

我分离了两个函数,因为每个函数都有两个不同的按钮,所以不强制首先执行createPDF,然后执行apendiceRemesa (我也将为两个函数开发一个新函数,然后删除一些冗余变量)。

谢谢!

%更新%%%

Yuri和Iamblichus sokution减少了约20%的执行时间。现在,我想知道是否有其他方法来识别工作表名称:

从…

代码语言:javascript
运行
复制
const currentSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CRM");

const data = currentSheet.getRange('al8:ar26').getDisplayValues();

像这样的事情:

代码语言:javascript
运行
复制
const data = getRange("CRM!AL8:AR26").getDisplayValues();
EN

Stack Overflow用户

发布于 2022-08-04 11:57:55

诀窍是将工作表中的所有数据作为2d数组获取,然后从数组中获取“单元格”,而不是直接从工作表中获取。

而不是这样:

代码语言:javascript
运行
复制
let profe_nombre    = currentSheet.getRange(14,40).getValues();
let profe_direccion = currentSheet.getRange(16,40).getValues();
let profe_dni       = currentSheet.getRange(15,40).getValues();
let profe_iban      = currentSheet.getRange(17,40).getValues();
let profe_paypal    = currentSheet.getRange(18,40).getValues();
let profe_email     = currentSheet.getRange(19,40).getValues();
let factura_nombre  = currentSheet.getRange(26,39).getValues();
let factura_fecha   = currentSheet.getRange(23,40).getDisplayValues();
let factura_importe = currentSheet.getRange(22,40).getValues();
let factura_notas   = currentSheet.getRange(26,38).getValues();

试试这个:

代码语言:javascript
运行
复制
const data = currentSheet.getDataRange().getDisplayValues(); // the 2d array

// take values from the array
let profe_nombre    = data[13][39];
let profe_direccion = data[15][39];
let profe_dni       = data[14][39];
let profe_iban      = data[16][39];
let profe_paypal    = data[17][39];
let profe_email     = data[18][39];
let factura_nombre  = data[25][38];
let factura_fecha   = data[22][39];
let factura_importe = data[21][39];
let factura_notas   = data[23][37];

等。

对于您的第二个函数apendiceRemesa()

代码语言:javascript
运行
复制
const data = SheetCRM.getDataRange().getValues();
let remesa_cuenta              = data[16][39];
let remesa_importe             = data[21][39];
let remesa_nombre              = data[13][39];
let remesa_concepto            = data[25][38];
const remesa_estado_pago       = data[23][39];
let remesa_estado_pago_fila    = data[22][42];
let remesa_estado_pago_columna = data[23][42];
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73235272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档