在 Google 个工作表中使用应用程序脚本移动单元格的内容
Moving contents of a cell with app script in Google sheets
Image of the lines of code对于我一直在做的事情,它涉及通过 appscript 或函数将图像从一个单元格移动到另一个单元格。我试图找出方法来做到这一点,但没有运气。有人有什么建议吗?例如,我需要将 A11 上的图像移动到 A34。
示例脚本 1:
关于For example, I would need to move an image on A11 to A34.
,如果您的图像放在单元格“A11”上,而您想将其移动到单元格“A34”,下面的示例脚本如何?
function sample1() {
const sheetName = "Sheet1"; // Please set the sheet name.
const fromCell = "A11";
const toCell = "A34";
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const image = sheet.getImages().filter(e => e.getAnchorCell().getA1Notation() == fromCell);
if (image.length == 0) return;
image[0].setAnchorCell(sheet.getRange(toCell));
}
示例脚本 2:
关于For example, I would need to move an image on A11 to A34.
,如果你的图像被放入单元格“A11”,而你想将它移动到单元格“A34”,下面的示例脚本怎么样?
function sample2() {
const sheetName = "Sheet1"; // Please set the sheet name.
const fromCell = "A11";
const toCell = "A34";
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const srcRange = sheet.getRange(fromCell);
srcRange.copyTo(sheet.getRange(toCell));
srcRange.clearContent();
}
注:
- 根据您的回复和提供的示例 Spreadsheet,我了解到您的
I would need to move an image on A11 to A34.
图片被放入了一个单元格中,而不是放在单元格上方。在这种情况下,我认为可以使用我的第二个脚本。当我使用我建议的第二个脚本测试你提供的样本 Spreadsheet 时,没有发生错误。单元格“A11”中的图像被移动到单元格“A34”。
- 从你
The second sample script, when I attempt to run it, I get the message "TypeError: Cannot read property 'getRange' of null".
的回复来看,例如,正如我提到的Please set the sheet name.
,如果你测试的Spreadsheet的sheet名称不是“Sheet1”,出现这样的错误。如果我的理解是正确的,请将const sheetName = "Sheet1";
的Sheet1
修改为您实际的sheet名称并重新测试
参考文献:
Image of the lines of code对于我一直在做的事情,它涉及通过 appscript 或函数将图像从一个单元格移动到另一个单元格。我试图找出方法来做到这一点,但没有运气。有人有什么建议吗?例如,我需要将 A11 上的图像移动到 A34。
示例脚本 1:
关于For example, I would need to move an image on A11 to A34.
,如果您的图像放在单元格“A11”上,而您想将其移动到单元格“A34”,下面的示例脚本如何?
function sample1() {
const sheetName = "Sheet1"; // Please set the sheet name.
const fromCell = "A11";
const toCell = "A34";
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const image = sheet.getImages().filter(e => e.getAnchorCell().getA1Notation() == fromCell);
if (image.length == 0) return;
image[0].setAnchorCell(sheet.getRange(toCell));
}
示例脚本 2:
关于For example, I would need to move an image on A11 to A34.
,如果你的图像被放入单元格“A11”,而你想将它移动到单元格“A34”,下面的示例脚本怎么样?
function sample2() {
const sheetName = "Sheet1"; // Please set the sheet name.
const fromCell = "A11";
const toCell = "A34";
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const srcRange = sheet.getRange(fromCell);
srcRange.copyTo(sheet.getRange(toCell));
srcRange.clearContent();
}
注:
- 根据您的回复和提供的示例 Spreadsheet,我了解到您的
I would need to move an image on A11 to A34.
图片被放入了一个单元格中,而不是放在单元格上方。在这种情况下,我认为可以使用我的第二个脚本。当我使用我建议的第二个脚本测试你提供的样本 Spreadsheet 时,没有发生错误。单元格“A11”中的图像被移动到单元格“A34”。 - 从你
The second sample script, when I attempt to run it, I get the message "TypeError: Cannot read property 'getRange' of null".
的回复来看,例如,正如我提到的Please set the sheet name.
,如果你测试的Spreadsheet的sheet名称不是“Sheet1”,出现这样的错误。如果我的理解是正确的,请将const sheetName = "Sheet1";
的Sheet1
修改为您实际的sheet名称并重新测试