如何使用 GAS 移动细胞图像?

How to move a Cell Image using GAS?

如何获取单元格中的单元格并移动到另一个文件中?

我正在尝试下面的方法,就像一只头晕目眩的蟑螂,但到目前为止没有成功:

function moveOrders () {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const orderSheet = ss.getSheetByName('Orders');
const activeSheetName = e.source.getActiveSheet().getName();
const thisRow = e.range.getRow();
const thisCol = e.range.getColumn();
const cellValue = e.range.getValue();
const destinationSheet = ss.getSheetByName('Sheet31');

if (activeSheetName == orderSheet.getName() && thisRow > 5 && thisCol < 13 && cellValue == 'Confirmed') {
    const rowValues = orderSheet.getRange(thisRow, 1, 1, 13).getValues();
    const image = SpreadsheetApp.newCellImage(rowValues[2]).setAltTextDescription('Clothing Piece').toBuilder().build();
    destinationSheet.getRange(destinationSheet+1,1,rowValues.length, rowValues[0].length).setValues(rowValues)
}

这就是我获取值的方式,图像以绿色突出显示(数组的第三个元素:

它抛出以下错误:

参数 null 与 SpreadsheetApp.newCellImage

的方法签名不对应

这是一个测试示例文件:https://docs.google.com/spreadsheets/d/1gxhXeNCMZ8MnJds1LM7vGQqibYgfXDYPkA6q6VNHcN0/edit?usp=sharing

我认为在您的情况下,此线程可能会有用。 而且,如果这反映在您的脚本中,那么以下修改如何?

而且,从 const activeSheetName = e.source.getActiveSheet().getName();const thisRow = e.range.getRow(); 等等,我猜想您可能想将您的脚本用作 OnEdit 触发器。

修改后的脚本:

function onEdit(e) {
  const orderSheet = e.source.getSheetByName('Orders');
  const activeSheetName = e.source.getActiveSheet().getName();
  const thisRow = e.range.getRow();
  const thisCol = e.range.getColumn();
  const cellValue = e.range.getValue();
  const destinationSheet = e.source.getSheetByName('Sheet31');
  if (activeSheetName == orderSheet.getName() && thisRow > 5 && thisCol < 13 && cellValue == 'Confirmed') {
    const rowValues = orderSheet.getRange(thisRow, 1, 1, 13);
    rowValues.copyTo(destinationSheet.getRange(destinationSheet.getLastRow() + 1, 1), { contentsOnly: true });
  }
}
  • 在您的显示脚本中,destinationSheet+1destinationSheet+ 是 Sheet 对象。在这种情况下,会发生错误。我猜你可能会使用 destinationSheet.getLastRow() + 1.

  • 当此脚本为 运行 时,该行使用 copyTo 从“订单”复制到“Sheet31”。这样,可以复制包含图像和值的行。

参考文献:

已添加

关于Does this work between files, as well?,修改上面的脚本后,变成如下。在这种情况下,请将 OnEdit 触发器安装到 installedOnEdit.

示例脚本:

function installedOnEdit(e) {
  const dstSpreadsheetId = "###"; // Please set the destination Spreadsheet ID.

  const orderSheet = e.source.getSheetByName('Orders');
  const activeSheetName = e.source.getActiveSheet().getName();
  const thisRow = e.range.getRow();
  const thisCol = e.range.getColumn();
  const cellValue = e.range.getValue();
  if (activeSheetName == orderSheet.getName() && thisRow > 5 && thisCol < 13 && cellValue == 'Confirmed') {
    const dstSS = SpreadsheetApp.openById(dstSpreadsheetId);
    const destinationSheet = dstSS.getSheetByName('Sheet31');
    const temp = orderSheet.copyTo(dstSS);
    const rowValues = temp.getRange(thisRow, 1, 1, 13);
    rowValues.copyTo(destinationSheet.getRange(destinationSheet.getLastRow() + 1, 1), { contentsOnly: true });
    dstSS.deleteSheet(temp);
  }
}

您可以只使用 moveTo(),不需要 getValues 和 setValues 这将从该范围和目标范围剪切和粘贴格式和值。

试试这个代码:

function onEdit(e) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const orderSheet = ss.getSheetByName('Orders');
  const activeSheetName = e.source.getActiveSheet().getName();
  const thisRow = e.range.getRow();
  const thisCol = e.range.getColumn();
  const cellValue = e.range.getValue();
  const destinationSheet = e.source.getSheetByName('Destination');
  if (activeSheetName == orderSheet.getName() && thisRow > 5 && thisCol < 13 && cellValue == 'Confirmed') {
    const rowValues = orderSheet.getRange(thisRow, 1, 1, 13);
    rowValues.moveTo(destinationSheet.getRange(destinationSheet.getLastRow() + 1, 1));
  }
}

让我知道这是否有效!

参考:https://developers.google.com/apps-script/reference/spreadsheet/range#movetotarget