如何在一個單元格中獲取一個單元格并移動到另一個檔案中?
我正在嘗試以下幾行,比如頭暈目眩的蟑螂,但到目前為止沒有成功:
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
uj5u.com熱心網友回復:
我認為在你的情況下,這個執行緒可能有用。Ref而且,如果這反映在您的腳本中,那么以下修改如何?
而且,從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 });
}
}
在您的顯示腳本中,
destinationSheetofdestinationSheet 1是 Sheet 物件。在這種情況下,會發生錯誤。我猜你可能會使用destinationSheet.getLastRow() 1.運行此腳本時,使用
copyTo. 這樣,可以復制包括影像和值的行。
參考:
- 相關執行緒。
- 在 Google 表格中使用應用腳本移動單元格的內容
- 復制到(目的地)
添加
關于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);
}
}
uj5u.com熱心網友回復:
您可以只使用 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/456279.html
上一篇:如何使用谷歌應用腳??本將日期/時間格式從yyyy-mm-ddT00:00:00Z轉換為yyyy-mm-dd00:00:00
