代碼行的影像對于我一直在做的事情,它涉及通過 appscript 或函式將影像從一個單元格移動到另一個單元格。我試圖找出方法來做到這一點,但沒有運氣。有人有什么建議嗎?例如,我需要將 A11 上的影像移動到 A34。
uj5u.com熱心網友回復:
示例腳本 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();
}
筆記:
- 從您的回復和提供的示例電子表格中,我可以理解您的影像
I would need to move an image on A11 to A34.被放入單元格而不是單元格上方。在這種情況下,我認為可以使用我的第二個腳本。當我使用我建議的第二個腳本測驗您提供的示例電子表格時,沒有發生錯誤。單元格“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.的,如果您測驗的電子表格的作業表名稱不是“Sheet1”,則會發生此類錯誤。如果我的理解是正確的,請修改Sheet1為const sheetName = "Sheet1";您的實際作業表名稱并再次測驗。
參考:
- 獲取影像()
- setAnchorCell(細胞)
- 類范圍的 copyTo(destination)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/450584.html
