javascript 转换语言Excel到谷歌工作表

oknrviil  于 2023-10-14  发布在  Java
关注(0)|答案(2)|浏览(78)

抱歉,如果这个线程已经被列出,或者如果我做错了什么原因,我不知道论坛,以及。
我有一段代码,我正在与之斗争。我最近开始使用谷歌表单,与使用Excel的Microsoft Excel表单相反,谷歌表单使用我不熟悉的Java编码。
所有我想做的,是移动一行从一个工作表到另一个一旦它已被标记为完成在第四个单元格。因此,要么移动它或复制它到第二张纸,并从原始工作表中删除它。
这是我的在线文档-https://docs.google.com/spreadsheets/d/1kzzSH5WaJFdU4I76CAjDKpBSfGK3iglZ3mGid00oldQ/edit?usp=sharing
下面是我在Excel中使用的代码:

Sub button()
    For Each myCell In Selection.Columns(4).Cells
   If myCell.Value = "Done" Then
    myCell.EntireRow.Copy Worksheets("Done").Range("A" & Rows.Count).End(3)(2)
   myCell.EntireRow.Delete
    End If
   Next
End Sub

先谢谢你了。
我试着找到一个Java语言转换器,但没有成功。我试着使用谷歌表格语言转换器,但它告诉我,我需要支付或订阅或什么,(我生活在第三世界国家,我更喜欢食物而不是奢侈品)。
我试着在谷歌上搜索代码,但无济于事,最后我试着写自己的代码,但没有成功。我很难找到时间来学习Java编码,因为我每天做两份工作来谋生。
再次抱歉,我在这里寻求帮助。
下面是我写的代码,并尝试了没有工作。

function Button(d) {
      var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
      var DoneSheet = activeSpreadsheet.getSheetByName('Done');
      var activeSheet = SpreadsheetApp.getActiveSheet();
      var numColumns = activeSheet.getLastColumn();
      var cell = d.range;
      var value = activeSheet.getLastColumn.value;
      var lock = LockService.getScriptLock();
 
           if (activeSheet.getSheetName() == 'Dev Jobs' && activeSheet.getLastColumn.value == 'Done') {
 
        lock.tryLock(10000);

        if (!lock.hasLock()) {
 
          activeSpreadsheet.toast('The Spreadsheet is busy currently, please try again in a bit.');
 
        } else {
     
      var row = activeSheet.getRange(cell.getRow(), 1, 1, numColumns).getValues();
 
      DoneSheet.appendRow(row[0]);
 
      activeSheet.deleteRow(cell.getRow());
 
        }
    }
}

我试过这个

if (activeSheet.getSheetName() == 'Dev Jobs' && activeSheet.getLastColumn.value == 'Done') {
 
    lock.tryLock(10000);
 
   if (!lock.hasLock()) {
 
      activeSpreadsheet.toast('The Spreadsheet is busy currently, please try again in a bit.');
 
   } else {
 
 var row = activeSheet.getRange(cell.getRow(), 1, 1, numColumns).getValues();
 
  DoneSheet.appendRow(row[0]);
 
  activeSheet.deleteRow(cell.getRow());
 
    }
  }
}
pbwdgjma

pbwdgjma1#

const SHEET_NAMES = {
  dev_jobs: "Dev Jobs",
  done: "Done"
};
function rows_move_status_done() {
  const SHEET_FROM_NAME = SHEET_NAMES.dev_jobs;
  const SHEET_TO_NAME = SHEET_NAMES.done;
  const EXPECTED_COLUMN_LABEL = "Status";
  const EXPECTED_COLUMN_VALUE = "Done";

  const sheet_from = SpreadsheetApp.getActiveSpreadsheet().getSheets().filter((sheet) => sheet.getName() == SHEET_FROM_NAME)[0];
  const sheet_to = SpreadsheetApp.getActiveSpreadsheet().getSheets().filter((sheet) => sheet.getName() == SHEET_TO_NAME)[0];

  const sheet_from_protection = sheet_from.protect().setWarningOnly(true);
  // if trying to edit, people will receive a warning asking for confirmation
  // it is to give enough time to avoid manual edits which could
  // change the data after rows is attained on next line, so moved data might is correct

  const rows = sheet_from.getDataRange().getValues();
  const status_index = rows[0].findIndex(value => value == EXPECTED_COLUMN_LABEL);
  //rows[0] is row number 1
  const remove_expected = (columns, row_number) => {
    if (columns[status_index] == EXPECTED_COLUMN_VALUE) {
      sheet_to.appendRow(columns);
      sheet_from.deleteRow(row_number);
      return true;
    }
    return false;
  }

  rows.forEach((value, index) => remove_expected(value, index + 1));
  //index + 1 to reflect excel expected row_number
  sheet_from_protection.remove();
}

function onEdit(e) {
  if (e.range.getSheet().getName() === SHEET_NAMES.dev_jobs) {
    rows_move_status_done();
  }
}
ryhaxcpt

ryhaxcpt2#

下面是将特定行移动到另一个工作表的示例。此过程仅适用于值,因为公式引用可能会中断。

function myTest() {
  try {
    let spread = SpreadsheetApp.getActiveSpreadsheet();
    let sheet1 = spread.getSheetByName("Sheet1");
    let sheet2 = spread.getSheetByName("Sheet2");
    let values = sheet1.getDataRange().getValues();
    // if you need to remove header then values.shift();
    let results = [];
    let rows = [];
    values.forEach( (row,index) => {
        if( row[3] === "Done" ) {  // Column D
          results.push(row);
          rows.unshift(index+1);  // which row needs to be deleted
        }
      }
    );
    sheet2.getRange(sheet2.getLastRow()+1,1,results.length,results[0].length).setValues(results);
    console.log(results);
    // now delete the rows from sheet1 starting with last
    rows.forEach( index => {
        sheet1.deleteRow(index);
      }
    );
  }
  catch(err) {
    console.log(err);
  }
}

参考资料

  • getSheetByName()
  • getDataRange()
  • Range.getValues()
  • Range.setValues()
  • System.out.println()
  • Spreadsheet Best Practices

相关问题