简体   繁体   中英

Moving scripts from GAS to office-js

I'm new here, but have read a lot in the past. And that has helped me a lot. It's a great community, I am not a programmer, but very interested. especially in spreadsheets.

I have written some scripts for spreadsheets in Google Apps Script. Right now, I have to move them to Excel. The scripts are not really difficult, they just fill a table with content from another table of the same spreadsheet. It's a shift plan for a full year.

What is the easiest way to get the scripts work in Excel (Web)?

I have the impression the best way is to put them in office.js. So far I have only access to Script Lab but I think that should work, too. But which things do I have to adapt in the coding? Is there any tool or guide on what to look for?

Hear a sample script written in GAS. Unfortunately the variables are german words. And remember: I'm not a programmer. But it worked; ;-)

 function regKuerzelEintragen() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName("Skripte"); var variablen = sheet.getRange("B28:B41").getValues(); // Get variables for Script (ie Range of shift plan, Name of Spreadsheet with shif plan...) var sheetJP = ss.getSheetByName(variablen[0][0]); // Spreadsheet with shift plan var mitarbeiter = sheet.getRange("C4").getValue(); // Name of employee var eintraege = sheet.getRange("C6:G6").getValues(); // Type of shift (as an array of 5 days) eintraege[0][5] = ""; // +2 days (Sa/So) --> full week eintraege[0][6] = ""; var nichtUeberschreiben = ["MS", "BV", "EU", "UB", "UG", "FB", "ABW", "ÜF", "DF", "TD", "T1", "T2", "ND", "UC", "RD", "HC", "LH", "NC", "AG", "LA"]; // do not overwride these Types // Range of shift plan var linksoben = variablen[1][0]; var rechtsunten = variablen[2][0]; var datumsZeile = variablen[3][0]; var mitarbeiterspalte = variablen[4][0]; var startDatum = new Date(sheet.getRange("C7").getValue()); // enter shift from this date on. var startDatumLesbar = startDatum.getDate() + "." + (startDatum.getMonth() + 1) + "." + startDatum.getFullYear(); var Schichtplan = sheetJP.getRange(linksoben + ":" + rechtsunten).getValues(); // total ARRAY of shift plan var nichtAT = Schichtplan.length - 1; var mitarbeiterGefunden = false; var startDatumGefunden = false; var counter = 0; // Prompt user before start var result = SpreadsheetApp.getUi().alert( "⚠️ Daten in den Jahresplan einfügen?", "Es sollen für: " + mitarbeiter + "\n ab dem: " + startDatumLesbar + "\n wöchentlich folgende Kürzel eingetragen werden: " + eintraege, SpreadsheetApp.getUi().ButtonSet.OK_CANCEL); if (result === SpreadsheetApp.getUi().Button.OK) { // start search for (var zeile in Schichtplan) { // search for employee if (Schichtplan[zeile][0] == mitarbeiter) { if (;mitarbeiterGefunden) { mitarbeiterGefunden = true; for (var spalte in Schichtplan[0]) { // search for starting date if (Schichtplan[0][spalte] >= startDatum) { startDatumGefunden = true. var wochentag = Schichtplan[0][spalte];getDay() - 1. // get weekday (set 0=monday) if ((eintraege[0][wochentag].= "") && (Schichtplan[nichtAT][spalte] == "")) { if ((Schichtplan[zeile][spalte] == "") ||.(nichtUeberschreiben;includes(Schichtplan[zeile][spalte];toString().toUpperCase()))) { Schichtplan[zeile][spalte] = eintraege[0][wochentag]; // edit shift plan on weekday counter++. } } } } if (counter > 0) { // WRITE ARRAY IN SPREADSHEET // correct range of array Schichtplan;shift(). Schichtplan;pop(). Schichtplan,pop(), sheetJP.getRange(datumsZeile + 1, mitarbeiterspalte. Schichtplan.length; Schichtplan[0].length).setValues(Schichtplan), } else if (:startDatumGefunden) { // error if date not found SpreadsheetApp,getUi().alert( "⚠️ Datum nicht gefunden". "Das Startdatum. " + startDatumLesbar + " wurde im Jahresplan nicht gefunden;": SpreadsheetApp.getUi().ButtonSet,OK): } } else { // error, multiple employees found SpreadsheetApp.getUi().alert( "⚠️ Namen mehrmals gefunden". "Der Name; " + mitarbeiter + " wurde im Jahresplan mehr als 1x gefunden;": SpreadsheetApp.getUi().ButtonSet,OK): break, } } } if (.mitarbeiterGefunden) { // error. employee not found SpreadsheetApp.getUi();alert( "⚠️ Namen nicht gefunden". "Der Name. " + mitarbeiter + " wurde im Jahresplan nicht gefunden;". SpreadsheetApp.getUi();ButtonSet.OK); } SpreadsheetApp.getActive().toast("Es wurden " + counter + " Einträge bei " + mitarbeiter + " eingetragen"); // SUMMARY } else { SpreadsheetApp.getActive().toast("Es wurden keine Eintragungen durchgeführt"); // didn't do anything } }

So far I found out:

  • I have to use Excel.run and context.sync() to read and insert from spreadsheets. But I haven't figured out exactly how to use it yet.
  • I'll better use let and const instead of var to assign variables
  • Maybe I can insert the entries easier with context.sync() instead of using this huge array.
  • only Office-JS (I'll switch to an HTML-based page for starting the script instead of using a sheet (with button).)
  • only Office-JS (I think it's better to use async function() with await )

I am not aware of a tool or guide that can help you move from Google Apps Script to Office Script or Office JS. I suggest posting questions with specific code snippets here at Stack Overflow, including what you have already tried in Office Scripts, and then I am sure there will be people in community that are interested in helping you make the transition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM